"rgb to hsl conversion" Code Answer


i've been reading several wiki pages and checking different calculations, and creating visualizations of rgb cube projection onto a hexagon. and i'd like to post my understanding of this conversion. since i find this conversion (representations of color models using geometric shapes) interesting, i'll try to be as thorough as i can be. first, let's start with rgb.


well, this doesn't really need much explanation. in its simplest form, you have 3 values, r, g, and b in the range of [0,255]. for example, 51,153,204. we can represent it using a bar graph:

rgb cube

we can also represent a color in a 3d space. we have three values r, g, b that corresponds to x, y, and z. all three values are in the [0,255] range, which results in a cube. but before creating the rgb cube, let's work on 2d space first. two combinations of r,g,b gives us: rg, rb, gb. if we were to graph these on a plane, we'd get the following:

these are the first three sides of the rgb cube. if we place them on a 3d space, it results in a half cube:

if you check the above graph, by mixing two colors, we get a new color at (255,255), and these are yellow, magenta, and cyan. again, two combinations of these gives us: ym, yc, and mc. these are the missing sides of the cube. once we add them, we get a complete cube:

and the position of 51,153,204 in this cube:

projection of rgb cube onto a hexagon

now that we have the rgb cube, let's project it onto a hexagon. first, we tilt the cube by 45° on the x, and then 35.264° on the y. after the second tilt, black corner is at the bottom and the white corner is at the top, and they both pass through the z axis.

as you can see, we get the hexagon look we want with the correct hue order when we look at the cube from the top. but we need to project this onto a real hexagon. what we do is draw a hexagon that is in the same size with the cube top view. all the corners of the hexagon corresponds to the corners of the cube and the colors, and the top corner of the cube that is white, is projected onto the center of the hexagon. black is omitted. and if we map every color onto the hexagon, we get the look at right.

and the position of 51,153,204 on the hexagon would be:

calculating the hue

before we make the calculation, let's define what hue is.

hue is roughly the angle of the vector to a point in the projection, with red at 0°.

... hue is how far around that hexagon’s edge the point lies.

this is the calculation from the hsl and hsv wiki page. we'll be using it in this explanation.

examine the hexagon and the position of 51,153,204 on it.

first, we scale the r, g, b values to fill the [0,1] interval.

r = r / 255    r =  51 / 255 = 0.2
g = g / 255    g = 153 / 255 = 0.6
b = b / 255    b = 204 / 255 = 0.8

next, find the max and min values of r, g, b

m = max(r, g, b)    m = max(0.2, 0.6, 0.8) = 0.8
m = min(r, g, b)    m = min(0.2, 0.6, 0.8) = 0.2

then, calculate c (chroma). chroma is defined as:

... chroma is roughly the distance of the point from the origin.

chroma is the relative size of the hexagon passing through a point ...

c = op / op'
c = m - m
c = 0.8- 0.2 = 0.6

now, we have the r, g, b, and c values. if we check the conditions, if m = b returns true for 51,153,204. so, we'll be using h'= (r - g) / c + 4.

let's check the hexagon again. (r - g) / c gives us the length of bp segment.

segment = (r - g) / c = (0.2 - 0.6) / 0.6 = -0.6666666666666666

we'll place this segment on the inner hexagon. starting point of the hexagon is r (red) at 0°. if the segment length is positive, it should be on ry, if negative, it should be on rm. in this case, it is negative -0.6666666666666666, and is on the rm edge.

next, we need to shift the position of the segment, or rather p₁ towars the b (because m = b). blue is at 240°. hexagon has 6 sides. each side corresponds to 60°. 240 / 60 = 4. we need to shift (increment) the p₁ by 4 (which is 240°). after the shift, p₁ will be at p and we'll get the length of rygcp.

segment = (r - g) / c = (0.2 - 0.6) / 0.6 = -0.6666666666666666
rygcp   = segment + 4 = 3.3333333333333335

circumference of the hexagon is 6 which corresponds to 360°. 53,151,204's distance to is 3.3333333333333335. if we multiply 3.3333333333333335 by 60, we'll get its position in degrees.

h' = 3.3333333333333335
h  = h' * 60 = 200°

in the case of if m = r, since we place one end of the segment at r (0°), we don't need to shift the segment to r if the segment length is positive. the position of p₁ will be positive. but if the segment length is negative, we need to shift it by 6, because negative value means that the angular position is greater than 180° and we need to do a full rotation.

so, neither the dutch wiki solution hue = (g - b) / c; nor the eng wiki solution hue = ((g - b) / c) % 6; will work for negative segment length. only the so answer hue = (g - b) / c + (g < b ? 6 : 0); works for both negative and positive values.

jsfiddle: test all three methods for rgb(255,71,99)

jsfiddle: find a color's position in rgb cube and hue hexagon visually

working hue calculation:


function rgb2hue(r, g, b) {
  r /= 255;
  g /= 255;
  b /= 255;
  var max = math.max(r, g, b);
  var min = math.min(r, g, b);
  var c   = max - min;
  var hue;
  if (c == 0) {
    hue = 0;
  } else {
    switch(max) {
      case r:
        var segment = (g - b) / c;
        var shift   = 0 / 60;       // r° / (360° / hex sides)
        if (segment < 0) {          // hue > 180, full rotation
          shift = 360 / 60;         // r° / (360° / hex sides)
        hue = segment + shift;
      case g:
        var segment = (b - r) / c;
        var shift   = 120 / 60;     // g° / (360° / hex sides)
        hue = segment + shift;
      case b:
        var segment = (r - g) / c;
        var shift   = 240 / 60;     // b° / (360° / hex sides)
        hue = segment + shift;
  return hue * 60; // hue is in [0,6], scale it up
By Acibi on March 23 2022

Answers related to “rgb to hsl conversion”

Only authorized users can answer the Search term. Please sign in first, or register a free account.