Postscript RGB and HSB color conversion.
Version of Wednesday 15 February 2017.
Dave Barber's other pages.

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:
• 0 ≤ red ≤ 1
• 0 ≤ grn ≤ 1
• 0 ≤ blu ≤ 1
Inputs are hue, sat, and bri, each a real number where:
• 0 ≤ hue ≤ 1
• 0 ≤ sat ≤ 1
• 0 ≤ bri ≤ 1
The hue value is cyclic. If hue = 1, its value is conventionally changed to 0.

Some sources differ in representing the hue, preferring degrees of arc with the following constraint:

• 0° ≤ hue ≤ 360°
Outputs are hue, sat, and bri, each a real number where:
• 0 ≤ hue < 1
• 0 ≤ sat ≤ 1
• 0 ≤ bri ≤ 1

Outputs are red, grn, and blu, each a real number where:

• 0 ≤ red ≤ 1
• 0 ≤ grn ≤ 1
• 0 ≤ blu ≤ 1
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:
• hue = 0 (by convention)
• sat = 0
• bri = red
When sat = 0, the result is also on the gray scale from black to white, and the outputs are:
• red = bri
• grn = bri
• blu = bri
The value of hue is ignored.
When red, grn, and blu are not all equal, define:
• min = minimum of red, grn, and blu
• max = maximum of red, grn, and blu
• mid = red + grn + blu − (max + min)
• quo = (midmin) ÷ (maxmin) ÷ 6
The conversion formula is then described in the twelve cases of table one.
When sat ≠ 0, define:
• pro = bri × sat
• dim = bripro
• mul = hue × 6
The conversion formula is then described in the twelve cases of table two.
table one
caseconditionhuesatbri
I. red > blu = grn0 ÷ 6 (maxmin) ÷ max max
II. red > grn > blu0 ÷ 6 + quo
III. red = grn > blu1 ÷ 6
IV. grn > red > blu2 ÷ 6 − quo
V. grn > red = blu2 ÷ 6
VI. grn > blu > red2 ÷ 6 + quo
VII. grn = blu > red3 ÷ 6
VIII.blu > grn > red4 ÷ 6 − quo
IX. blu > grn = red4 ÷ 6
X. blu > red > grn4 ÷ 6 + quo
XI. blu = red > grn5 ÷ 6
XII. red > blu > grn6 ÷ 6 − quo

The red = grn = blu case does not fit into table one, because calculations would entail division by zero.

table two
caseconditionredgrnblu
I. mul = 0bridimdim
II. 0 < mul < 1bridim + pro × (mul − 0)dim
III. mul = 1bribridim
IV. 1 < mul < 2dim + pro × (2 − mul)bridim
V. mul = 2dimbridim
VI. 2 < mul < 3dimbridim + pro × (mul − 2)
VII. mul = 3dimbribri
VIII.3 < mul < 4dimdim + pro × (4 − mul)bri
IX. mul = 4dimdimbri
X. 4 < mul < 5dim + pro × (mul − 4)dimbri
XI. mul = 5bridimbri
XII. 5 < mul < 6bridimdim + pro × (6 − mul)

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.