style: format code (#4212)

close #4204
This commit is contained in:
acbin
2023-06-09 18:52:05 +08:00
committed by GitHub
parent ad03086f54
commit 00282efd8b
521 changed files with 5233 additions and 7309 deletions

View File

@ -19,69 +19,30 @@ public class RgbHsvConversion {
// Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
// Test hsvToRgb-method
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] { 0, 0, 0 });
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] { 255, 255, 255 });
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] { 255, 0, 0 });
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] { 255, 255, 0 });
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] { 0, 255, 0 });
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] { 0, 0, 255 });
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] { 255, 0, 255 });
assert Arrays.equals(
hsvToRgb(180, 0.5, 0.5),
new int[] { 64, 128, 128 }
);
assert Arrays.equals(
hsvToRgb(234, 0.14, 0.88),
new int[] { 193, 196, 224 }
);
assert Arrays.equals(
hsvToRgb(330, 0.75, 0.5),
new int[] { 128, 32, 80 }
);
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] {0, 0, 0});
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] {255, 255, 255});
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] {255, 0, 0});
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] {255, 255, 0});
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] {0, 255, 0});
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] {0, 0, 255});
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] {255, 0, 255});
assert Arrays.equals(hsvToRgb(180, 0.5, 0.5), new int[] {64, 128, 128});
assert Arrays.equals(hsvToRgb(234, 0.14, 0.88), new int[] {193, 196, 224});
assert Arrays.equals(hsvToRgb(330, 0.75, 0.5), new int[] {128, 32, 80});
// Test rgbToHsv-method
// approximate-assertions needed because of small deviations due to converting between
// int-values and double-values.
assert approximatelyEqualHsv(
rgbToHsv(0, 0, 0),
new double[] { 0, 0, 0 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 255, 255),
new double[] { 0, 0, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 0, 0),
new double[] { 0, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 255, 0),
new double[] { 60, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(0, 255, 0),
new double[] { 120, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(0, 0, 255),
new double[] { 240, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 0, 255),
new double[] { 300, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(64, 128, 128),
new double[] { 180, 0.5, 0.5 }
);
assert approximatelyEqualHsv(
rgbToHsv(193, 196, 224),
new double[] { 234, 0.14, 0.88 }
);
assert approximatelyEqualHsv(
rgbToHsv(128, 32, 80),
new double[] { 330, 0.75, 0.5 }
);
assert approximatelyEqualHsv(rgbToHsv(0, 0, 0), new double[] {0, 0, 0});
assert approximatelyEqualHsv(rgbToHsv(255, 255, 255), new double[] {0, 0, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 0, 0), new double[] {0, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 255, 0), new double[] {60, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[] {120, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[] {240, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[] {300, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[] {180, 0.5, 0.5});
assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88});
assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5});
}
/**
@ -94,35 +55,23 @@ public class RgbHsvConversion {
*/
public static int[] hsvToRgb(double hue, double saturation, double value) {
if (hue < 0 || hue > 360) {
throw new IllegalArgumentException(
"hue should be between 0 and 360"
);
throw new IllegalArgumentException("hue should be between 0 and 360");
}
if (saturation < 0 || saturation > 1) {
throw new IllegalArgumentException(
"saturation should be between 0 and 1"
);
throw new IllegalArgumentException("saturation should be between 0 and 1");
}
if (value < 0 || value > 1) {
throw new IllegalArgumentException(
"value should be between 0 and 1"
);
throw new IllegalArgumentException("value should be between 0 and 1");
}
double chroma = value * saturation;
double hueSection = hue / 60;
double secondLargestComponent =
chroma * (1 - Math.abs(hueSection % 2 - 1));
double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));
double matchValue = value - chroma;
return getRgbBySection(
hueSection,
chroma,
matchValue,
secondLargestComponent
);
return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);
}
/**
@ -135,21 +84,15 @@ public class RgbHsvConversion {
*/
public static double[] rgbToHsv(int red, int green, int blue) {
if (red < 0 || red > 255) {
throw new IllegalArgumentException(
"red should be between 0 and 255"
);
throw new IllegalArgumentException("red should be between 0 and 255");
}
if (green < 0 || green > 255) {
throw new IllegalArgumentException(
"green should be between 0 and 255"
);
throw new IllegalArgumentException("green should be between 0 and 255");
}
if (blue < 0 || blue > 255) {
throw new IllegalArgumentException(
"blue should be between 0 and 255"
);
throw new IllegalArgumentException("blue should be between 0 and 255");
}
double dRed = (double) red / 255;
@ -172,7 +115,7 @@ public class RgbHsvConversion {
hue = (hue + 360) % 360;
return new double[] { hue, saturation, value };
return new double[] {hue, saturation, value};
}
private static boolean approximatelyEqualHsv(double[] hsv1, double[] hsv2) {
@ -184,11 +127,7 @@ public class RgbHsvConversion {
}
private static int[] getRgbBySection(
double hueSection,
double chroma,
double matchValue,
double secondLargestComponent
) {
double hueSection, double chroma, double matchValue, double secondLargestComponent) {
int red;
int green;
int blue;
@ -219,7 +158,7 @@ public class RgbHsvConversion {
blue = convertToInt(secondLargestComponent + matchValue);
}
return new int[] { red, green, blue };
return new int[] {red, green, blue};
}
private static int convertToInt(double input) {