The following is are procedures for converting between the red-green-blue (RGB) color model and the hue-saturation-brightness (HSB a.k.a. HSV) color model, as implemented in the Abobe PostScript language.
These color models are widely employed on computers, and are certainly convenient, but they do not attempt to capture the full subtlety of human color perception, an area that continues to be an active field of scientific research.
The procedures are described in parallel columns with an emphasis on explicative clarity rather than computational efficiency. Users may of course streamline the steps as desired.
|converting from RGB to HSB||converting from HSB to RGB|
|Inputs are red, grn, and blu, each a real number where:||Inputs are hue, sat, and bri, each a real number where:|
Some sources differ in representing the hue, preferring degrees of arc with the following constraint:
|Outputs are hue, sat, and bri, each a real number where:|
Outputs are red, grn, and blu, each a real number where:
|When red, grn, and blu are all equal, the result is on the gray scale from black (bri = 0) to white (bri = 1), and the outputs are:||When sat = 0, the result is also on the gray scale from black to white, and the outputs are:|
|When red, grn, and blu are not all equal, define:||When sat ≠ 0, define:|
The red = grn = blu case does not fit into table one, because calculations would entail division by zero.
The sat = 0 case does not fit into table two, because the case discriminator mul is based on hue, whose value is immaterial when sat = 0.
|Between table one and table two, cases with the same Roman numeral produce the same subset of colors.|