Dada la gama de colores RGB, nuestra tarea es convertir el color RGB en color HSV.
Modelo de color RGB:
el modelo de color RGB es un modelo de color aditivo en el que la luz roja, verde y azul se suman de varias maneras para reproducir una amplia gama de colores. El nombre del modelo proviene de las iniciales de los tres colores primarios aditivos, rojo, verde y azul.
Modelo de color HSV: Los
artistas suelen utilizar HSV (tono, saturación, valor), también conocido como HSB (tono, saturación, brillo), porque suele ser más natural pensar en un color en términos de tono y saturación que en términos de componentes de color aditivos o sustractivos. HSV es una transformación de un espacio de color RGB, y sus componentes y colorimetría son relativos al espacio de color RGB del que se deriva.
Ejemplos:
Input : r, g, b = 45, 215, 0 Output : h, s, v = 107.44186046511628, 100.0, 84.31372549019608 Input : r, g, v = 31, 52, 29 Output : h, s, v = 114.78260869565217, 44.230769230769226, 20.392156862745097
Acercarse :
- Divide r, g, b por 255
- Calcular cmax, cmin, diferencia
- Cálculo de tono:
- si cmax y cmin son iguales a 0, entonces h = 0
- si cmax es igual a r entonces calcule h = (60 * ((g – b) / diff) + 360) % 360
- si cmax es igual a g entonces calcule h = (60 * ((b – r) / diff) + 120) % 360
- si cmax es igual a b entonces calcule h = (60 * ((r – g) / diff) + 240) % 360
- Cálculo de la saturación:
- si cmax = 0, entonces s = 0
- si cmax no es igual a 0, calcule s = (diff/cmax)*100
- Cálculo del valor:
- v = cmáx*100
A continuación se muestra la implementación del enfoque anterior:
Java
// Java program change RGB Color // Model to HSV Color Model class GFG { static void rgb_to_hsv(double r, double g, double b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value double cmax = Math.max(r, Math.max(g, b)); // maximum of r, g, b double cmin = Math.min(r, Math.min(g, b)); // minimum of r, g, b double diff = cmax - cmin; // diff of cmax and cmin. double h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = (60 * ((g - b) / diff) + 360) % 360; // if cmax equal g then compute h else if (cmax == g) h = (60 * ((b - r) / diff) + 120) % 360; // if cmax equal b then compute h else if (cmax == b) h = (60 * ((r - g) / diff) + 240) % 360; // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v double v = cmax * 100; System.out.println("(" + h + " " + s + " " + v + ")"); } // Driver Code public static void main(String[] args) { // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); } } // This code is contributed by PrinciRaj1992
Python3
# Python3 program change RGB Color # Model to HSV Color Model def rgb_to_hsv(r, g, b): # R, G, B values are divided by 255 # to change the range from 0..255 to 0..1: r, g, b = r / 255.0, g / 255.0, b / 255.0 # h, s, v = hue, saturation, value cmax = max(r, g, b) # maximum of r, g, b cmin = min(r, g, b) # minimum of r, g, b diff = cmax-cmin # diff of cmax and cmin. # if cmax and cmax are equal then h = 0 if cmax == cmin: h = 0 # if cmax equal r then compute h elif cmax == r: h = (60 * ((g - b) / diff) + 360) % 360 # if cmax equal g then compute h elif cmax == g: h = (60 * ((b - r) / diff) + 120) % 360 # if cmax equal b then compute h elif cmax == b: h = (60 * ((r - g) / diff) + 240) % 360 # if cmax equal zero if cmax == 0: s = 0 else: s = (diff / cmax) * 100 # compute v v = cmax * 100 return h, s, v ''' Driver Code ''' # print(rgb_to_hsv(45, 215, 0)) # print(rgb_to_hsv(31, 52, 29)) print(rgb_to_hsv(129, 88, 47))
C#
// C# program change RGB Color // Model to HSV Color Model using System; class GFG { static void rgb_to_hsv(double r, double g, double b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value double cmax = Math.Max(r, Math.Max(g, b)); // maximum of r, g, b double cmin = Math.Min(r, Math.Min(g, b)); // minimum of r, g, b double diff = cmax - cmin; // diff of cmax and cmin. double h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = (60 * ((g - b) / diff) + 360) % 360; // if cmax equal g then compute h else if (cmax == g) h = (60 * ((b - r) / diff) + 120) % 360; // if cmax equal b then compute h else if (cmax == b) h = (60 * ((r - g) / diff) + 240) % 360; // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v double v = cmax * 100; Console.WriteLine("(" + h + " " + s + " " + v + ")"); } // Driver Code public static void Main(String[] args) { // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); } } // This code is contributed by Rajput-Ji
Javascript
<script> // javascript program change RGB Color // Model to HSV Color Model function rgb_to_hsv(r , g , b) { // R, G, B values are divided by 255 // to change the range from 0..255 to 0..1 r = r / 255.0; g = g / 255.0; b = b / 255.0; // h, s, v = hue, saturation, value var cmax = Math.max(r, Math.max(g, b)); // maximum of r, g, b var cmin = Math.min(r, Math.min(g, b)); // minimum of r, g, b var diff = cmax - cmin; // diff of cmax and cmin. var h = -1, s = -1; // if cmax and cmax are equal then h = 0 if (cmax == cmin) h = 0; // if cmax equal r then compute h else if (cmax == r) h = (60 * ((g - b) / diff) + 360) % 360; // if cmax equal g then compute h else if (cmax == g) h = (60 * ((b - r) / diff) + 120) % 360; // if cmax equal b then compute h else if (cmax == b) h = (60 * ((r - g) / diff) + 240) % 360; // if cmax equal zero if (cmax == 0) s = 0; else s = (diff / cmax) * 100; // compute v var v = cmax * 100; document.write("(" + h.toFixed(1) + ", " + s + ", " + v + ")"); } // Driver Code // rgb_to_hsv(45, 215, 0); // rgb_to_hsv(31, 52, 29); rgb_to_hsv(129, 88, 47); // This code is contributed by todaysgaurav </script>
Producción :
(30.0, 63.56589147286821, 50.588235294117645)
Publicación traducida automáticamente
Artículo escrito por shrikanth13 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA