Color Conversion
RGB to HSV
HSV to RGB
Algorithm
Color Model

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In the realm of computer graphics and image processing, color representations are crucial. Two commonly used color spaces are RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value). Understanding how to convert between these two color spaces is essential for tasks such as color correction, image filtering, and graphic design software applications. This article provides an in-depth explanation of the algorithms for converting RGB to HSV and HSV to RGB, with both representations scaled in the range of 0-255.

RGB and HSV Color Spaces

RGB Color Space

The RGB color space is a straightforward model used primarily in electronic displays. Each color is a combination of red, green, and blue components, each ranging between 0 and 255. The combination of these three components covers a broad spectrum of colors.

Example:

  • (255, 0, 0) represents red.
  • (0, 255, 0) represents green.
  • (0, 0, 255) represents blue.

HSV Color Space

The HSV color space models colors in terms of hue, saturation, and value. This can be more intuitive for humans as it decouples the color intensity (value) from the color's shade (hue) and vividness (saturation).

  • Hue (H) ranges from 0 to 255. It represents the color type (e.g., red, green, blue) and is measured in degrees on the color wheel.
  • Saturation (S) ranges from 0 to 255. It describes the intensity or purity of the color.
  • Value (V) ranges from 0 to 255. It indicates the brightness of the color.

Conversion: RGB to HSV

To convert from RGB to HSV, follow these steps:

  1. Normalize RGB values: Convert the RGB components from a range of 0-255 to 0-1 by dividing each by 255 so r = R ÷ 255, g = G ÷ 255, and b = B ÷ 255.
  2. Determine the maximum and minimum values: Let Cmax be the maximum of r, g, and b. Let Cmin be the minimum of those values. Define delta as Cmax - Cmin.
  3. Calculate Hue (H):
    • If delta equals 0, then set H = 0.
    • If Cmax comes from r, compute H = 60 × ((g - b) ÷ delta mod 6).
    • If Cmax comes from g, compute H = 60 × (((b - r) ÷ delta) + 2).
    • If Cmax comes from b, compute H = 60 × (((r - g) ÷ delta) + 4).
  4. Calculate Saturation (S):
    • If Cmax equals 0, then S = 0.
    • Otherwise, S = (delta ÷ Cmax) × 255.
  5. Calculate Value (V): Set V = Cmax × 255.
  6. Scale Hue to 0-255: Rescale H by computing (H ÷ 360) × 255.

The resulting H, S, V tuple represents the color in the HSV color space.

Conversion: HSV to RGB

To convert from HSV to RGB, follow these steps:

  1. Normalize HSV values: Convert the HSV components by scaling H = (H ÷ 255) × 360, S = S ÷ 255, and V = V ÷ 255.
  2. Calculate Chroma (C): Compute C = V × S.
  3. Calculate Intermediate Values: Let Hprime = H ÷ 60. Then compute X = C × (1 - |(Hprime mod 2) - 1|).
  4. Set RGB Prime Components based on Hue:
    • If 0 ≤ Hprime < 1: set (R_prime, G_prime, B_prime) = (C, X, 0).
    • If 1 ≤ Hprime < 2: set (R_prime, G_prime, B_prime) = (X, C, 0).
    • If 2 ≤ Hprime < 3: set (R_prime, G_prime, B_prime) = (0, C, X).
    • If 3 ≤ Hprime < 4: set (R_prime, G_prime, B_prime) = (0, X, C).
    • If 4 ≤ Hprime < 5: set (R_prime, G_prime, B_prime) = (X, 0, C).
    • If 5 ≤ Hprime < 6: set (R_prime, G_prime, B_prime) = (C, 0, X).
  5. Calculate Match Value (m): Set m = V - C.
  6. Convert to final RGB values: Convert back to the 0-255 scale with R = (R' + m) × 255, G = (G' + m) × 255, and B = (B' + m) × 255.

The resulting R, G, B tuple represents the color in the RGB color space.

Summary Table

OperationFormula/Steps
RGB Normalizationr = R ÷ 255, g = G ÷ 255, b = B ÷ 255
HSV NormalizationH = (H ÷ 255) × 360, S = S ÷ 255, V = V ÷ 255
RGB to HSV - Max/MinCmax = max(r, g, b), Cmin = min(r, g, b), delta = Cmax - Cmin
RGB to HSV - HIf delta = 0, H = 0; otherwise compute hue from the appropriate color channel
RGB to HSV - SIf Cmax = 0, S = 0; otherwise S = (delta ÷ Cmax) × 255
RGB to HSV - VV = Cmax × 255
HSV to RGB - ChromaC = V × S
HSV to RGB - H to RGB PrimesDetermine (R', G', B') based on the hue segment
Final RGB ConversionR = (R' + m) × 255, G = (G' + m) × 255, B = (B' + m) × 255

Conclusion

Understanding the conversion between RGB and HSV color spaces is fundamental for various applications in digital imaging and graphics. The algorithms provided here ensure accurate and reversible transformations, crucial for engineers and software developers working in visual technologies. By using these conversions, one can manipulate colors more intuitively and effectively in their applications.


Course illustration
Course illustration

All Rights Reserved.