Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created February 14, 2017 15:22
Show Gist options
  • Save treytomes/7bbefe050c000395f92825b49b71eb5d to your computer and use it in GitHub Desktop.
Save treytomes/7bbefe050c000395f92825b49b71eb5d to your computer and use it in GitHub Desktop.
Convert to and from html colors, rgb <-->hsl colors, and darken html colors by a percentage.
-- Convert an html color in the format of #RRGGBB into red, green, and blue values on the range of [0..255].
local function hex_to_rgb(html_color)
html_color = html_color:gsub("#", "")
return tonumber("0x"..html_color:sub(1, 2)), tonumber("0x"..html_color:sub(3, 4)), tonumber("0x"..html_color:sub(5, 6))
end
-- Convert the red, green, and blue color components back into an html color string.
local function rgb_to_hex(red, green, blue)
return string.format("#%02X%02X%02X", red, green, blue)
end
-- Convert red, green, and blue values from the range of [0..255] into hue, saturation, and value/lightness values on the range of...?
local function rgb_to_hsv(red, green, blue)
local r = red / 255
local g = green / 255
local b = blue / 255
local max = math.max(r, g, b)
local min = math.min(r, g, b)
local h, s, v = max, max, max
local d = max - min;
if max ~= 0 then
s = d / max
end
if max == min then
h = 0 -- achromatic
else
if max == r then
if g < b then
h = (g - b) / d + 6
else
h = (g - b) / d + 0
end
elseif max == g then
h = (b - r) / d + 2
elseif max == b then
h = (r - g) / d + 4
end
h = h / 6
end
return h, s, v
end
--[[
Convert the hue, saturation, and value/lightness values output from rgb_to_hsv
back into red, green, and blue values on the range of [0..255].
]]
local function hsv_to_rgb(hue, saturation, value)
local i = math.floor(hue * 6)
local f = hue * 6 - i
local p = value * (1 - saturation)
local q = value * (1 - f * saturation)
local t = value * (1 - (1 - f) * saturation)
local red, green, blue
local cases = {
[0] = function() red, green, blue = value, t, p end,
[1] = function() red, green, blue = q, value, p end,
[2] = function() red, green, blue = p, value, t end,
[3] = function() red, green, blue = p, q, value end,
[4] = function() red, green, blue = t, p, value end,
[5] = function() red, green, blue = value, p, q end
}
cases[i % 6]()
return math.floor(red * 255 + 0.5), math.floor(green * 255 + 0.5), math.floor(blue * 255 + 0.5)
end
-- Make the input html color darker by the given percent.
local function darken_color(html_color, percent)
local r, g, b = hex_to_rgb(html_color)
local h, s, v = rgb_to_hsv(r, g, b)
v = v * (1 - percent)
r, g, b = hsv_to_rgb(h, s, v)
return rgb_to_hex(r, g, b)
end
@treytomes
Copy link
Author

I put these functions together for a Minetest mod, but they turned out to not be necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment