From f39d712a915b837fe34bb74ab880a1879fc6eaf1 Mon Sep 17 00:00:00 2001 From: Leon Heidelbach Date: Sat, 4 Jun 2022 21:29:31 +0200 Subject: [PATCH] minor cleanup --- lua/colors.lua | 54 +++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/lua/colors.lua b/lua/colors.lua index af07655..e605317 100644 --- a/lua/colors.lua +++ b/lua/colors.lua @@ -2,14 +2,12 @@ local M = {} -- Convert a hex color value to RGB -- @param hex: The hex color value --- @return r: red (0-255) --- @return g: green (0-255) --- @return b: blue (0-255) +-- @return r: Red (0-255) +-- @return g: Green (0-255) +-- @return b: Blue (0-255) M.hex2rgb = function(hex) local hash = string.sub(hex, 1, 1) == "#" - if string.len(hex) ~= (7 - (hash and 0 or 1)) then - return nil - end + if string.len(hex) ~= (7 - (hash and 0 or 1)) then return nil end local r = tonumber(hex:sub(2 - (hash and 0 or 1), 3 - (hash and 0 or 1)), 16) local g = tonumber(hex:sub(4 - (hash and 0 or 1), 5 - (hash and 0 or 1)), 16) @@ -18,9 +16,9 @@ M.hex2rgb = function(hex) end -- Convert an RGB color value to hex --- @param r The red value (0-255) --- @param g The green value (0-255) --- @param b The blue value (0-255) +-- @param r: Red (0-255) +-- @param g: Green (0-255) +-- @param b: Blue (0-255) -- @return The hexadecimal string representation of the color M.rgb2hex = function(r, g, b) return string.format("#%02x%02x%02x", math.floor(r), math.floor(g), math.floor(b)) @@ -38,12 +36,12 @@ M.hsl2rgb_helper = function(p, q, a) end -- Convert a HSL color value to RGB --- @param h: hue (0-360) --- @param s: saturation (0-1) --- @param l: lightness (0-1) --- @return r: red (0-255) --- @return g: green (0-255) --- @return b: blue (0-255) +-- @param h: Hue (0-360) +-- @param s: Saturation (0-1) +-- @param l: Lightness (0-1) +-- @return r: Red (0-255) +-- @return g: Green (0-255) +-- @return b: Blue (0-255) M.hsl2rgb = function(h, s, l) local t1, t2, r, g, b @@ -58,6 +56,7 @@ M.hsl2rgb = function(h, s, l) r = M.hsl2rgb_helper(t1, t2, h + 2) * 255 g = M.hsl2rgb_helper(t1, t2, h) * 255 b = M.hsl2rgb_helper(t1, t2, h - 2) * 255 + return r, g, b end @@ -76,9 +75,9 @@ M.rgb2hsl = function(r, g, b) max = math.max(r, g, b) maxcolor = 1 + (max == b and 2 or (max == g and 1 or 0)) - if maxcolor == 1 then h = (g - b) / (max - min) end - if maxcolor == 2 then h = 2 + (b - r) / (max - min) end - if maxcolor == 3 then h = 4 + (r - g) / (max - min) end + if maxcolor == 1 then h = (g - b) / (max - min) + elseif maxcolor == 2 then h = 2 + (b - r) / (max - min) + elseif maxcolor == 3 then h = 4 + (r - g) / (max - min) end if not rawequal(type(h), 'number') then h = 0 end @@ -91,11 +90,8 @@ M.rgb2hsl = function(r, g, b) if min == max then s = 0 else - if l < 0.5 then - s = (max - min) / (max + min) - else - s = (max - min) / (2 - max - min) - end + if l < 0.5 then s = (max - min) / (max + min) + else s = (max - min) / (2 - max - min) end end return h, s, l @@ -103,18 +99,18 @@ end -- Convert a hex color value to HSL -- @param hex: The hex color value --- @return h: Hue --- @return s: Saturation --- @return l: Lightness +-- @param h: Hue (0-360) +-- @param s: Saturation (0-1) +-- @param l: Lightness (0-1) M.hex2hsl = function(hex) local r, g, b = M.hex2rgb(hex) return M.rgb2hsl(r, g, b) end -- Convert a HSL color value to hex --- @param h Hue --- @param s Saturation --- @param l Lightness +-- @param h: Hue (0-360) +-- @param s: Saturation (0-1) +-- @param l: Lightness (0-1) -- @returns hex color value M.hsl2hex = function(h, s, l) local r, g, b = M.hsl2rgb(h, s, l)