|
| 1 | +packageConversions; |
| 2 | + |
| 3 | +importjava.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * The RGB color model is an additive color model in which red, green, and blue light are added |
| 7 | + * together in various ways to reproduce a broad array of colors. The name of the model comes from |
| 8 | + * the initials of the three additive primary colors, red, green, and blue. Meanwhile, the HSV |
| 9 | + * representation models how colors appear under light. In it, colors are represented using three |
| 10 | + * components: hue, saturation and (brightness-)value. This class provides methods for converting |
| 11 | + * colors from one representation to the other. (description adapted from |
| 12 | + * https://en.wikipedia.org/wiki/RGB_color_model and https://en.wikipedia.org/wiki/HSL_and_HSV). |
| 13 | + */ |
| 14 | +publicclassRgbHsvConversion { |
| 15 | + |
| 16 | +publicstaticvoidmain(String[]args) { |
| 17 | +// Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html |
| 18 | + |
| 19 | +// Test hsvToRgb-method |
| 20 | +assertArrays.equals(hsvToRgb(0,0,0),newint[] {0,0,0}); |
| 21 | +assertArrays.equals(hsvToRgb(0,0,1),newint[] {255,255,255}); |
| 22 | +assertArrays.equals(hsvToRgb(0,1,1),newint[] {255,0,0}); |
| 23 | +assertArrays.equals(hsvToRgb(60,1,1),newint[] {255,255,0}); |
| 24 | +assertArrays.equals(hsvToRgb(120,1,1),newint[] {0,255,0}); |
| 25 | +assertArrays.equals(hsvToRgb(240,1,1),newint[] {0,0,255}); |
| 26 | +assertArrays.equals(hsvToRgb(300,1,1),newint[] {255,0,255}); |
| 27 | +assertArrays.equals(hsvToRgb(180,0.5,0.5),newint[] {64,128,128}); |
| 28 | +assertArrays.equals(hsvToRgb(234,0.14,0.88),newint[] {193,196,224}); |
| 29 | +assertArrays.equals(hsvToRgb(330,0.75,0.5),newint[] {128,32,80}); |
| 30 | + |
| 31 | +// Test rgbToHsv-method |
| 32 | +// approximate-assertions needed because of small deviations due to converting between |
| 33 | +// int-values and double-values. |
| 34 | +assertapproximatelyEqualHsv(rgbToHsv(0,0,0),newdouble[] {0,0,0}); |
| 35 | +assertapproximatelyEqualHsv(rgbToHsv(255,255,255),newdouble[] {0,0,1}); |
| 36 | +assertapproximatelyEqualHsv(rgbToHsv(255,0,0),newdouble[] {0,1,1}); |
| 37 | +assertapproximatelyEqualHsv(rgbToHsv(255,255,0),newdouble[] {60,1,1}); |
| 38 | +assertapproximatelyEqualHsv(rgbToHsv(0,255,0),newdouble[] {120,1,1}); |
| 39 | +assertapproximatelyEqualHsv(rgbToHsv(0,0,255),newdouble[] {240,1,1}); |
| 40 | +assertapproximatelyEqualHsv(rgbToHsv(255,0,255),newdouble[] {300,1,1}); |
| 41 | +assertapproximatelyEqualHsv(rgbToHsv(64,128,128),newdouble[] {180,0.5,0.5}); |
| 42 | +assertapproximatelyEqualHsv(rgbToHsv(193,196,224),newdouble[] {234,0.14,0.88}); |
| 43 | +assertapproximatelyEqualHsv(rgbToHsv(128,32,80),newdouble[] {330,0.75,0.5}); |
| 44 | + } |
| 45 | + |
| 46 | +/** |
| 47 | + * Conversion from the HSV-representation to the RGB-representation. |
| 48 | + * |
| 49 | + * @param hue Hue of the color. |
| 50 | + * @param saturation Saturation of the color. |
| 51 | + * @param value Brightness-value of the color. |
| 52 | + * @return The tuple of RGB-components. |
| 53 | + */ |
| 54 | +publicstaticint[]hsvToRgb(doublehue,doublesaturation,doublevalue) { |
| 55 | +if (hue <0 ||hue >360) { |
| 56 | +thrownewIllegalArgumentException("hue should be between 0 and 360"); |
| 57 | + } |
| 58 | + |
| 59 | +if (saturation <0 ||saturation >1) { |
| 60 | +thrownewIllegalArgumentException("saturation should be between 0 and 1"); |
| 61 | + } |
| 62 | + |
| 63 | +if (value <0 ||value >1) { |
| 64 | +thrownewIllegalArgumentException("value should be between 0 and 1"); |
| 65 | + } |
| 66 | + |
| 67 | +doublechroma =value *saturation; |
| 68 | +doublehueSection =hue /60; |
| 69 | +doublesecondLargestComponent =chroma * (1 -Math.abs(hueSection %2 -1)); |
| 70 | +doublematchValue =value -chroma; |
| 71 | + |
| 72 | +returngetRgbBySection(hueSection,chroma,matchValue,secondLargestComponent); |
| 73 | + } |
| 74 | + |
| 75 | +/** |
| 76 | + * Conversion from the RGB-representation to the HSV-representation. |
| 77 | + * |
| 78 | + * @param red Red-component of the color. |
| 79 | + * @param green Green-component of the color. |
| 80 | + * @param blue Blue-component of the color. |
| 81 | + * @return The tuple of HSV-components. |
| 82 | + */ |
| 83 | +publicstaticdouble[]rgbToHsv(intred,intgreen,intblue) { |
| 84 | +if (red <0 ||red >255) { |
| 85 | +thrownewIllegalArgumentException("red should be between 0 and 255"); |
| 86 | + } |
| 87 | + |
| 88 | +if (green <0 ||green >255) { |
| 89 | +thrownewIllegalArgumentException("green should be between 0 and 255"); |
| 90 | + } |
| 91 | + |
| 92 | +if (blue <0 ||blue >255) { |
| 93 | +thrownewIllegalArgumentException("blue should be between 0 and 255"); |
| 94 | + } |
| 95 | + |
| 96 | +doubledRed = (double)red /255; |
| 97 | +doubledGreen = (double)green /255; |
| 98 | +doubledBlue = (double)blue /255; |
| 99 | +doublevalue =Math.max(Math.max(dRed,dGreen),dBlue); |
| 100 | +doublechroma =value -Math.min(Math.min(dRed,dGreen),dBlue); |
| 101 | +doublesaturation =value ==0 ?0 :chroma /value; |
| 102 | +doublehue; |
| 103 | + |
| 104 | +if (chroma ==0) { |
| 105 | +hue =0; |
| 106 | + }elseif (value ==dRed) { |
| 107 | +hue =60 * (0 + (dGreen -dBlue) /chroma); |
| 108 | + }elseif (value ==dGreen) { |
| 109 | +hue =60 * (2 + (dBlue -dRed) /chroma); |
| 110 | + }else { |
| 111 | +hue =60 * (4 + (dRed -dGreen) /chroma); |
| 112 | + } |
| 113 | + |
| 114 | +hue = (hue +360) %360; |
| 115 | + |
| 116 | +returnnewdouble[] {hue,saturation,value}; |
| 117 | + } |
| 118 | + |
| 119 | +privatestaticbooleanapproximatelyEqualHsv(double[]hsv1,double[]hsv2) { |
| 120 | +booleanbHue =Math.abs(hsv1[0] -hsv2[0]) <0.2; |
| 121 | +booleanbSaturation =Math.abs(hsv1[1] -hsv2[1]) <0.002; |
| 122 | +booleanbValue =Math.abs(hsv1[2] -hsv2[2]) <0.002; |
| 123 | + |
| 124 | +returnbHue &&bSaturation &&bValue; |
| 125 | + } |
| 126 | + |
| 127 | +privatestaticint[]getRgbBySection( |
| 128 | +doublehueSection,doublechroma,doublematchValue,doublesecondLargestComponent) { |
| 129 | +intred; |
| 130 | +intgreen; |
| 131 | +intblue; |
| 132 | + |
| 133 | +if (hueSection >=0 &&hueSection <=1) { |
| 134 | +red =convertToInt(chroma +matchValue); |
| 135 | +green =convertToInt(secondLargestComponent +matchValue); |
| 136 | +blue =convertToInt(matchValue); |
| 137 | + }elseif (hueSection >1 &&hueSection <=2) { |
| 138 | +red =convertToInt(secondLargestComponent +matchValue); |
| 139 | +green =convertToInt(chroma +matchValue); |
| 140 | +blue =convertToInt(matchValue); |
| 141 | + }elseif (hueSection >2 &&hueSection <=3) { |
| 142 | +red =convertToInt(matchValue); |
| 143 | +green =convertToInt(chroma +matchValue); |
| 144 | +blue =convertToInt(secondLargestComponent +matchValue); |
| 145 | + }elseif (hueSection >3 &&hueSection <=4) { |
| 146 | +red =convertToInt(matchValue); |
| 147 | +green =convertToInt(secondLargestComponent +matchValue); |
| 148 | +blue =convertToInt(chroma +matchValue); |
| 149 | + }elseif (hueSection >4 &&hueSection <=5) { |
| 150 | +red =convertToInt(secondLargestComponent +matchValue); |
| 151 | +green =convertToInt(matchValue); |
| 152 | +blue =convertToInt(chroma +matchValue); |
| 153 | + }else { |
| 154 | +red =convertToInt(chroma +matchValue); |
| 155 | +green =convertToInt(matchValue); |
| 156 | +blue =convertToInt(secondLargestComponent +matchValue); |
| 157 | + } |
| 158 | + |
| 159 | +returnnewint[] {red,green,blue}; |
| 160 | + } |
| 161 | + |
| 162 | +privatestaticintconvertToInt(doubleinput) { |
| 163 | +return (int)Math.round(255 *input); |
| 164 | + } |
| 165 | +} |