+ public double[] toHSV() {
+ return toHSV(r / 255.0, g / 255.0, b / 255.0);
+ }
+
+ /**
+ * @return an array with hsv values ranging from 0.0 to 1.0
+ */
+ public static double[] toHSV(double r, double g, double b) {
+ double h, s, v;
+ double min = Math.min(Math.min(r, g), b);
+ double max = Math.max(Math.max(r, g), b);
+
+ if (max == 0) {
+ return new double[] {0, 0, 0};
+ }
+
+ // Value
+ v = max;
+
+ // Saturation
+ double delta = max - min;
+ s = delta / max;
+
+ // Hue
+ if (r == max) {
+ h = (g - b) / delta; // between yellow & magenta
+ } else if (g == max) {
+ h = 2 + (b - r) / delta; // between cyan & yellow
+ } else {
+ h = 4 + (r - g) / delta; // between magenta & cyan
+ }
+
+ h /= 6.0;
+ if (h < 0)
+ h += 1;
+
+ return new double[] {h, s, v};
+ }
+