v9.1.1
Menu

RGB to HSV Conversion

mediumFREEColor SpacesComputer Vision 2

Given a single RGB pixel with values in [0, 255], convert it to HSV color space.

Algorithm:

  1. Normalize R, G, B to [0, 1] by dividing by 255
  2. Compute Cmax=max⁡(R′,G′,B′)C_{max} = \max(R', G', B'), Cmin=min⁡(R′,G′,B′)C_{min} = \min(R', G', B'), Δ=Cmax−Cmin\Delta = C_{max} - C_{min}
  3. Hue (0-360 degrees):
    • If Δ=0\Delta = 0: H=0H = 0
    • If Cmax=R′C_{max} = R': H=60×(((G′−B′)/Δ)mod  6)H = 60 \times (((G' - B') / \Delta) \mod 6)
    • If Cmax=G′C_{max} = G': H=60×(((B′−R′)/Δ)+2)H = 60 \times (((B' - R') / \Delta) + 2)
    • If Cmax=B′C_{max} = B': H=60×(((R′−G′)/Δ)+4)H = 60 \times (((R' - G') / \Delta) + 4)
  4. Saturation (0-1): S=0S = 0 if Cmax=0C_{max} = 0, else Δ/Cmax\Delta / C_{max}
  5. Value (0-1): V=CmaxV = C_{max}

Return [H,S,V][H, S, V] with H rounded to 2 decimal places, S and V rounded to 4 decimal places.

Example

Input
pixel = [255, 0, 0]
Output
[0.0, 1.0, 1.0]
Reasoning
  • Input: pixel holds one colour as three integer intensities in [0,255][0, 255]: red 255255 (full), green 00, blue 00. That is pure, fully bright red.
  • Normalise: dividing each by 255255 gives R′=1R' = 1, G′=0G' = 0, B′=0B' = 0. So Cmax=1C_{max} = 1 (from red), Cmin=0C_{min} = 0 and Δ=1−0=1\Delta = 1 - 0 = 1.
  • Hue: red is the largest channel, so the red formula applies: H=60×(0−01 mod 6)=60×0=0∘H = 60 \times \left(\frac{0 - 0}{1} \bmod 6\right) = 60 \times 0 = 0^\circ. Hue is an angle on the colour wheel, and red sits at 0∘0^\circ (green at 120∘120^\circ, blue at 240∘240^\circ).
  • Saturation: S=Δ/Cmax=1/1=1S = \Delta / C_{max} = 1/1 = 1, meaning no grey mixed in. Value: V=Cmax=1V = C_{max} = 1, maximum brightness.
  • Output: [0.0, 1.0, 1.0], as floats after rounding (H to 2 places, S and V to 4).
  • Notice that a darker red such as [128,0,0][128, 0, 0] keeps H=0H = 0 and S=1S = 1; only VV drops, to 128/255≈0.502128/255 \approx 0.502. Brightness lives entirely in VV.

Constraints

  • Pixel is [R, G, B] with integer values in [0, 255]
  • H in [0, 360), S in [0, 1], V in [0, 1]
  • If max = 0, then H = 0 and S = 0
  • Return list [H, S, V]

Learn this in

More from Computer Vision 2

solution.py

Test results

Sign in to run your code against the test cases.