Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thealgorithms.conversions;

import java.util.Arrays;

/**
* The RGB color model is an additive color model in which red, green, and blue
* light are added together in various ways to reproduce a broad array of
Expand All @@ -17,36 +15,6 @@ public final class RgbHsvConversion {
private RgbHsvConversion() {
}

public static void main(String[] args) {
// 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});

// 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});
}

/**
* Conversion from the HSV-representation to the RGB-representation.
*
Expand Down Expand Up @@ -120,14 +88,6 @@ public static double[] rgbToHsv(int red, int green, int blue) {
return new double[] {hue, saturation, value};
}

private static boolean approximatelyEqualHsv(double[] hsv1, double[] hsv2) {
boolean bHue = Math.abs(hsv1[0] - hsv2[0]) < 0.2;
boolean bSaturation = Math.abs(hsv1[1] - hsv2[1]) < 0.002;
boolean bValue = Math.abs(hsv1[2] - hsv2[2]) < 0.002;

return bHue && bSaturation && bValue;
}

private static int[] getRgbBySection(double hueSection, double chroma, double matchValue, double secondLargestComponent) {
int red;
int green;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.thealgorithms.conversions;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class RgbHsvConversionTest {

// Expected values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
@ParameterizedTest
@CsvSource({"0, 0, 0, 0, 0, 0", "0, 0, 1, 255, 255, 255", "0, 1, 1, 255, 0, 0", "60, 1, 1, 255, 255, 0", "120, 1, 1, 0, 255, 0", "240, 1, 1, 0, 0, 255", "300, 1, 1, 255, 0, 255", "180, 0.5, 0.5, 64, 128, 128", "234, 0.14, 0.88, 193, 196, 224", "330, 0.75, 0.5, 128, 32, 80"})
void testHsvToRgb(double hue, double saturation, double value, int red, int green, int blue) {
assertArrayEquals(new int[] {red, green, blue}, RgbHsvConversion.hsvToRgb(hue, saturation, value));
}

// Small deviations are expected because the RGB components are integers.
@ParameterizedTest
@CsvSource({"0, 0, 0, 0, 0, 0", "255, 255, 255, 0, 0, 1", "255, 0, 0, 0, 1, 1", "255, 255, 0, 60, 1, 1", "0, 255, 0, 120, 1, 1", "0, 0, 255, 240, 1, 1", "255, 0, 255, 300, 1, 1", "64, 128, 128, 180, 0.5, 0.5", "193, 196, 224, 234, 0.14, 0.88", "128, 32, 80, 330, 0.75, 0.5"})
void testRgbToHsv(int red, int green, int blue, double hue, double saturation, double value) {
double[] hsv = RgbHsvConversion.rgbToHsv(red, green, blue);
assertEquals(hue, hsv[0], 0.2);
assertEquals(saturation, hsv[1], 0.002);
assertEquals(value, hsv[2], 0.002);
}

@ParameterizedTest
@CsvSource({"0, 1, 1", "60, 0.5, 0.75", "200, 0.3, 0.6", "359, 1, 0.5"})
void testRoundTrip(double hue, double saturation, double value) {
int[] rgb = RgbHsvConversion.hsvToRgb(hue, saturation, value);
double[] hsv = RgbHsvConversion.rgbToHsv(rgb[0], rgb[1], rgb[2]);
assertEquals(hue, hsv[0], 1.0);
assertEquals(saturation, hsv[1], 0.01);
assertEquals(value, hsv[2], 0.01);
}

@Test
void testHsvToRgbRejectsOutOfRangeValues() {
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.hsvToRgb(-1, 0.5, 0.5));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.hsvToRgb(361, 0.5, 0.5));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.hsvToRgb(180, -0.1, 0.5));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.hsvToRgb(180, 1.1, 0.5));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.hsvToRgb(180, 0.5, -0.1));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.hsvToRgb(180, 0.5, 1.1));
}

@Test
void testRgbToHsvRejectsOutOfRangeValues() {
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.rgbToHsv(-1, 0, 0));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.rgbToHsv(256, 0, 0));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.rgbToHsv(0, -1, 0));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.rgbToHsv(0, 256, 0));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.rgbToHsv(0, 0, -1));
assertThrows(IllegalArgumentException.class, () -> RgbHsvConversion.rgbToHsv(0, 0, 256));
}
}