Skip to content

Commit

Permalink
feat(color): add WCAG AA contrast validation
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenEast committed Jan 5, 2023
1 parent a637479 commit 12e0ca7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lua/nightfox/lib/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ function Color:to_css(with_alpha)
return string.format("#%0" .. l .. "x", n)
end

---Calculate the relative lumanance of the color
---https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
---@return number
function Color:lumanance()
local r, g, b = self.red, self.green, self.blue
r = (r > 0.04045) and ((r + 0.055) / 1.055) ^ 2.4 or (r / 12.92)
g = (g > 0.04045) and ((g + 0.055) / 1.055) ^ 2.4 or (g / 12.92)
b = (b > 0.04045) and ((b + 0.055) / 1.055) ^ 2.4 or (b / 12.92)

return 0.2126 * r + 0.7152 * g + 0.0722 * b
end

--#endregion

--#region Manipulate -----------------------------------------------------------
Expand Down Expand Up @@ -297,6 +309,30 @@ Color.BLACK = Color.init(0, 0, 0, 1)

--#endregion

--#region Utility --------------------------------------------------------------

---Returns the contrast ratio of the other against another
---@param other Color
function Color:contrast(other)
local l1 = self:lumanance()
local l2 = other:lumanance()
if l2 > l1 then
l1, l2 = l2, l1
end
return (l1 + 0.05) / (l2 + 0.05)
end

---Check if color passes WCAG AA
---https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html
---@param background Color background to check against
---@return boolean, number
function Color:valid_wcag_aa(background)
local ratio = self:contrast(background)
return ratio >= 4.5, ratio
end

--#endregion

local mt = getmetatable(Color)
function mt.__call(_, opts)
if type(opts) == "string" or type(opts) == "number" then
Expand Down

0 comments on commit 12e0ca7

Please sign in to comment.