This is an interesting problem. This cannot be avoided with the android class due to the low accuracy of the float. However, I found a similar solution written in javascript here .
If it's important enough that you want to define your own method / class for the conversion, here is a Java conversion that should give you better accuracy:
@Size(3) /** Does the same as {@link android.graphics.Color#colorToHSV(int, float[])} */ public double[] colorToHSV(@ColorInt int color) { //this line copied vertabim return rgbToHsv((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF); } @Size(3) public double[] rgbToHsv(double r, double g, double b) { final double max = Math.max(r, Math.max(g, b)); final double min = Math.min(r, Math.min(g, b)); final double diff = max - min; final double h; final double s = ((max == 0d)? 0d : diff / max); final double v = max / 255d; if (min == max) { h = 0d; } else if (r == max) { double tempH = (g - b) + diff * (g < b ? 6: 0); tempH /= 6 * diff; h = tempH; } else if (g == max) { double tempH = (b - r) + diff * 2; tempH /= 6 * diff; h = tempH; } else { double tempH = (r - g) + diff * 4; tempH /= 6 * diff; h = tempH; } return new double[] { h, s, v }; }
I have to admit ignorance here - I did a quick conversion and did not have time to check correctly. There may be a better solution, but that should make you start at least.
source share