mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -19,30 +19,69 @@ 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 }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,23 +94,35 @@ 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
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,15 +135,21 @@ 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;
|
||||
@ -115,7 +172,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) {
|
||||
@ -127,7 +184,11 @@ 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;
|
||||
@ -158,7 +219,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) {
|
||||
|
Reference in New Issue
Block a user