Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. -- http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
  2.  
  3. --[[
  4.  * Converts an RGB color value to HSL. Conversion formula
  5.  * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
  6.  * Assumes r, g, and b are contained in the set [0, 255] and
  7.  * returns h, s, and l in the set [0, 1].
  8.  *
  9.  * @param   Number  r       The red color value
  10.  * @param   Number  g       The green color value
  11.  * @param   Number  b       The blue color value
  12.  * @return  Array           The HSL representation
  13. ]]
  14. function rgbToHsl(r, g, b, a)
  15.   r, g, b = r / 255, g / 255, b / 255
  16.  
  17.   local max, min = math.max(r, g, b), math.min(r, g, b)
  18.   local h, s, l
  19.  
  20.   l = (max + min) / 2
  21.  
  22.   if max == min then
  23.     h, s = 0, 0 -- achromatic
  24.   else
  25.     local d = max - min
  26.     local s
  27.     if l > 0.5 then s = d / (2 - max - min) else s = d / (max + min) end
  28.     if max == r then
  29.       h = (g - b) / d
  30.       if g < b then h = h + 6 end
  31.     elseif max == g then h = (b - r) / d + 2
  32.     elseif max == b then h = (r - g) / d + 4
  33.     end
  34.     h = h / 6
  35.   end
  36.  
  37.   return h, s, l, a or 255
  38. end
  39.  
  40. --[[
  41.  * Converts an HSL color value to RGB. Conversion formula
  42.  * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
  43.  * Assumes h, s, and l are contained in the set [0, 1] and
  44.  * returns r, g, and b in the set [0, 255].
  45.  *
  46.  * @param   Number  h       The hue
  47.  * @param   Number  s       The saturation
  48.  * @param   Number  l       The lightness
  49.  * @return  Array           The RGB representation
  50. ]]
  51. function hslToRgb(h, s, l, a)
  52.   local r, g, b
  53.  
  54.   if s == 0 then
  55.     r, g, b = l, l, l -- achromatic
  56.   else
  57.     function hue2rgb(p, q, t)
  58.       if t < 0   then t = t + 1 end
  59.       if t > 1   then t = t - 1 end
  60.       if t < 1/6 then return p + (q - p) * 6 * t end
  61.       if t < 1/2 then return q end
  62.       if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
  63.       return p
  64.     end
  65.  
  66.     local q
  67.     if l < 0.5 then q = l * (1 + s) else q = l + s - l * s end
  68.     local p = 2 * l - q
  69.  
  70.     r = hue2rgb(p, q, h + 1/3)
  71.     g = hue2rgb(p, q, h)
  72.     b = hue2rgb(p, q, h - 1/3)
  73.   end
  74.  
  75.   return r * 255, g * 255, b * 255, a * 255
  76. end
  77.  
  78. --[[
  79.  * Converts an RGB color value to HSV. Conversion formula
  80.  * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
  81.  * Assumes r, g, and b are contained in the set [0, 255] and
  82.  * returns h, s, and v in the set [0, 1].
  83.  *
  84.  * @param   Number  r       The red color value
  85.  * @param   Number  g       The green color value
  86.  * @param   Number  b       The blue color value
  87.  * @return  Array           The HSV representation
  88. ]]
  89. function rgbToHsv(r, g, b, a)
  90.   r, g, b, a = r / 255, g / 255, b / 255, a / 255
  91.   local max, min = math.max(r, g, b), math.min(r, g, b)
  92.   local h, s, v
  93.   v = max
  94.  
  95.   local d = max - min
  96.   if max == 0 then s = 0 else s = d / max end
  97.  
  98.   if max == min then
  99.     h = 0 -- achromatic
  100.   else
  101.     if max == r then
  102.     h = (g - b) / d
  103.     if g < b then h = h + 6 end
  104.     elseif max == g then h = (b - r) / d + 2
  105.     elseif max == b then h = (r - g) / d + 4
  106.     end
  107.     h = h / 6
  108.   end
  109.  
  110.   return h, s, v, a
  111. end
  112.  
  113. --[[
  114.  * Converts an HSV color value to RGB. Conversion formula
  115.  * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
  116.  * Assumes h, s, and v are contained in the set [0, 1] and
  117.  * returns r, g, and b in the set [0, 255].
  118.  *
  119.  * @param   Number  h       The hue
  120.  * @param   Number  s       The saturation
  121.  * @param   Number  v       The value
  122.  * @return  Array           The RGB representation
  123. ]]
  124. function hsvToRgb(h, s, v, a)
  125.   local r, g, b
  126.  
  127.   local i = Math.floor(h * 6);
  128.   local f = h * 6 - i;
  129.   local p = v * (1 - s);
  130.   local q = v * (1 - f * s);
  131.   local t = v * (1 - (1 - f) * s);
  132.  
  133.   i = i % 6
  134.  
  135.   if i == 0 then r, g, b = v, t, p
  136.   elseif i == 1 then r, g, b = q, v, p
  137.   elseif i == 2 then r, g, b = p, v, t
  138.   elseif i == 3 then r, g, b = p, q, v
  139.   elseif i == 4 then r, g, b = t, p, v
  140.   elseif i == 5 then r, g, b = v, p, q
  141.   end
  142.  
  143.   return r * 255, g * 255, b * 255, a * 255
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement