dieter.nvim/lua/dieter/hsl.lua

71 lines
1.7 KiB
Lua

-- https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua
local M = {}
--- Converts an HSL color value to RGB. Conversion formula
--- adapted from http://en.wikipedia.org/wiki/HSL_color_space.
--- Assumes h, s, and l are contained in the set [0, 1] and
--- returns r, g, and b in the set [0, 255].
---
--- @param h number The hue
--- @param s number The saturation
--- @param l number The lightness
--- @return number, number, number # The RGB representation
function M.hslToRgb(h, s, l)
--- @type number, number, number
local r, g, b
if s == 0 then
r, g, b = l, l, l -- achromatic
else
--- @param p number
--- @param q number
--- @param t number
local function hue2rgb(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < 1 / 6 then
return p + (q - p) * 6 * t
end
if t < 1 / 2 then
return q
end
if t < 2 / 3 then
return p + (q - p) * (2 / 3 - t) * 6
end
return p
end
--- @type number
local q
if l < 0.5 then
q = l * (1 + s)
else
q = l + s - l * s
end
local p = 2 * l - q
r = hue2rgb(p, q, h + 1 / 3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1 / 3)
end
return r * 255, g * 255, b * 255
end
--- Converts an HSL color value to RGB in Hex representation.
--- @param h number The hue
--- @param s number The saturation
--- @param l number The lightness
--- @return string # The hex representation
function M.hslToHex(h, s, l)
local r, g, b = M.hslToRgb(h / 360, s / 100, l / 100)
return string.format("#%02x%02x%02x", r, g, b)
end
return M