Java examples for 2D Graphics:Color RGB
Converts an RGB array to an HSV array
/**/* w w w.ja v a 2s . c o m*/ * CamanJ - Java Image Manipulation * Ported from the CamanJS Javascript library * * Copyright 2011, Ryan LeFevre * Licensed under the new BSD License * See LICENSE for more info. * * Project Home: http://github.com/meltingice/CamanJ */ public class Main{ public static void main(String[] argv) throws Exception{ int[] rgb = new int[]{34,35,36,37,37,37,67,68,69}; System.out.println(java.util.Arrays.toString(rgbToHsv(rgb))); } /** * Converts an RGB array to an HSV array * * @param rgb * The RGB values to convert * @return An array of double values representing the equivalent HSV value */ public static double[] rgbToHsv(int[] rgb) { double[] drgb = CamanUtil.toDouble(rgb); drgb[0] /= 255; drgb[1] /= 255; drgb[2] /= 255; double max = Math.max(Math.max(drgb[0], drgb[1]), drgb[2]); double min = Math.min(Math.min(drgb[0], drgb[1]), drgb[2]); double h = max, s = max, v = max; double d = max - min; s = (max == 0 ? 0 : d / max); if (max == min) { // Monochrome h = 0; } else { if (max == rgb[0]) { h = (rgb[1] - rgb[2]) / d + (rgb[1] < rgb[2] ? 6 : 0); } else if (max == rgb[1]) { h = (rgb[2] - rgb[0]) / d + 2; } else if (max == rgb[2]) { h = (rgb[0] - rgb[1]) / d + 4; } h /= 6; } return new double[] { h, s, v }; } }