The RGB-to-HSV conversion algorithm divides each 8-bit RGB channel by 255, sets value to the largest normalized channel, calculates saturation from the channel spread, and derives hue from the dominant channel.

HSV reorganizes the same color around three properties that are often easier to adjust independently: hue identifies a position on the color wheel, saturation measures color intensity relative to value, and value represents the strongest RGB channel. The transformation does not assign a subjective color name or determine whether a color is suitable for a particular design.

For a direct browser workflow, the RGB to HSV Converter accepts whole-number channels from 0 through 255 and displays hue in degrees, saturation and value as percentages, normalized HSV notation, a HEX code, and a visual swatch.

convert rgb to hsv algorithm
Convert RGB to HSV Algorithm: An Implementation Blueprint

Define the implementation contract

A reliable implementation for an 8-bit sRGB-style color follows a fixed sequence:

  • Accept finite whole-number red, green, and blue channels from 0 through 255.
  • Normalize each channel to the range from 0 to 1.
  • Find the maximum, minimum, and difference between them.
  • Calculate value and saturation from those measurements.
  • Resolve hue from the maximal channel, with a separate achromatic branch.
  • Format the final components for the application that will consume them.

The order matters. Normalization keeps all calculations on one scale, while the separate achromatic branch prevents a division by zero when every channel is equal.

Normalize the RGB triplet

For 8-bit input, divide each channel by 255 before calculating the maximum, minimum, or channel spread:

r = R / 255, g = G / 255, b = B / 255

For example, a channel value of 255 becomes 1, while a channel value of 0 remains 0. Mixing normalized values with original 0–255 values would distort both the saturation ratio and the hue calculation.

Keep full floating-point precision during the calculation and round only when preparing the display. The browser converter follows this rule: its internal JavaScript result remains unrounded until the final formatting step.

Calculate value and saturation

After normalization, define three intermediate measurements:

  • m = max(r, g, b), the largest normalized channel.
  • n = min(r, g, b), the smallest normalized channel.
  • d = m − n, the spread between the largest and smallest channels.

Value is simply V = m. Saturation is S = d / m when the maximum is greater than zero. When all channels are zero, set saturation to zero instead of dividing by zero.

For a nonzero gray color, the maximum and minimum are equal, so d is zero and saturation is also zero. HSV value is not the same as HSL lightness: HSV value follows the strongest RGB channel, while HSL lightness uses the average of the maximum and minimum channels.

Choose the hue branch

When d is zero, hue has no visual meaning because the color is achromatic. The browser converter reports the conventional placeholder hue of 0 degrees. When d is greater than zero, use the maximal channel to select the correct branch:

  • If m = r, calculate H = 60 × ((g − b) / d).
  • If m = g, calculate H = 60 × ((b − r) / d + 2).
  • If m = b, calculate H = 60 × ((r − g) / d + 4).

After the branch, wrap any negative hue into the nonnegative range by adding 360 degrees. A modulo operation provides the same final range when implemented consistently. If channels tie for the maximum, use a fixed branch order so the implementation remains deterministic.

The resulting hue follows the familiar color-wheel positions: red begins at 0 degrees, yellow at 60, green at 120, cyan at 180, blue at 240, and magenta at 300.

Build and test one complete conversion

Consider RGB (120, 60, 30). Normalizing gives r = 8/17, g = 4/17, and b = 2/17. The maximum is 8/17, the minimum is 2/17, and the difference is 6/17.

Value is 8/17, or approximately 47.06%. Saturation is (6/17) divided by (8/17), which equals 75%. Because red is maximal, hue is 60 × ((4/17 − 2/17) divided by (6/17)), which equals 20°. The final result is H = 20°, S = 75%, V = 47.06%.

Convert RGB values with the browser tool

  1. Collect the source red, green, and blue values, confirming that each is a whole number from 0 through 255.
  2. Open the RGB to HSV Converter and enter the three channel values in their corresponding fields.
  3. Select Convert to HSV.
  4. Read hue in degrees and saturation and value as percentages. The interface also shows normalized HSV notation, the source HEX code, and a color swatch, with two decimal places used for display.
  5. Check whether the destination expects degrees and percentages, zero-to-one fractions, or a compact integer range before transferring the values.

The calculation runs entirely in the browser and does not save a color history. The original inputs remain visible, so you can change one channel and compare how the three HSV components respond.

Match the destination's HSV representation

The same HSV color can be stored under several numeric conventions. Check the destination's documentation before inserting values:

Destination convention Hue format Saturation and value format Required adjustment
Degrees and percentages 0° up to but not including 360° 0–100% Use the converter's displayed degrees and percentages.
Degrees and fractions Degrees 0–1 Divide the displayed saturation and value percentages by 100.
Fully normalized fractions 0–1 0–1 Divide hue degrees by 360 and saturation and value percentages by 100 when the destination specifies normalized hue.
Compact integers Destination-specific range Destination-specific range Apply the documented integer mapping instead of pasting the displayed values directly.

Because the interface rounds for readability, a fraction reconstructed from a displayed percentage is approximate. Some image-processing software stores hue in a 0–179 or 0–255 integer range, so the destination's scale must be confirmed rather than assumed.

If a destination expects HSL, use an HSL conversion workflow instead of relabeling the HSV value component. The two models arrange the same RGB triplet differently.

Guard inputs and special cases

The browser tool implements a stricter 8-bit contract than the underlying mathematics may require:

  • Only finite whole-number channels from 0 through 255 are accepted.
  • Decimal values, empty fields, negative numbers, and values above 255 produce an explicit error rather than being silently rounded or clamped.
  • Black has value zero, saturation zero, and the placeholder hue of zero degrees.
  • Any color with equal red, green, and blue channels has saturation zero and the placeholder hue of zero degrees.
  • Alpha is outside the conversion's scope because transparency does not change the HSV calculation for the underlying RGB channels.

The converter operates on the supplied sRGB-style triplet. It does not read embedded image profiles, convert between wide-gamut color spaces, or guarantee that another application will round a result in exactly the same way.

Coordinate conversion also does not establish text readability. For accessible interface work, the RGB to HSV conversion and accessibility contrast guide explains why the actual foreground and background pair must be tested separately.

Check library-specific scales

Named libraries can define their own output scales. The Android Color.colorToHSV documentation uses hue from zero up to but not including 360 degrees, with saturation and value from 0 to 1. The R grDevices rgb2hsv documentation uses normalized components, including hue as a fraction rather than degrees.

The browser converter's implementation is independently checked against the R convention for typical 0–255 sRGB input. Its golden cases cover the six primary and secondary wheel positions, black, and middle gray, providing a practical baseline before adding application-specific formatting or rounding.

For a deeper look, see Convert RGB to HSV Formula: Max, Delta, and Hue Math.