-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set vim.o.background
#77
Comments
I'm open to suggestions, but there's no good way to automate |
I like the idea of setting How about the following method of determining whether a color scheme has a
Edit: I wrote an example in Lua, but it's awkward since I'm new to Lua. Lua Script#!/usr/bin/env lua
--- A demonstration to determine whether a color scheme is light or dark.
-- @module demo
--- Parses a hexadecimal sRGB color and returns a table containing three
-- components: red, green, and blue, each in the range [0.0, 1.0].
-- @tparam string hex An sRGB color represented in the hexadecimal format #RRGGBB.
-- @treturn {float,float,float} A table containing the red, green, and blue
-- components of the given color, normalized to the range [0.0, 1.0].
-- @usage
-- local result = srgb("#FFFFFF")
-- local r = result[1]
-- local g = result[2]
-- local b = result[3]
local function srgb(hex)
local stripped = string.sub(hex, 2) -- Removes the prefix "#"
local srgb = tonumber(stripped, 16)
return {
(srgb >> 16 & 0XFF) / 255.0, -- red
(srgb >> 8 & 0XFF) / 255.0, -- green
(srgb & 0XFF) / 255.0, -- blue
}
end
--- Converts sRGB to linear RGB.
-- @tparam {float,float,float} srgb An sRGB color with each component normalized
-- to the range [0.0, 1.0].
-- @treturn {float,float,float} A table containing the red, green, and blue
-- components of the given linear RGB color.
-- @usage
-- local normalized_srgb = { 1.0, 1.0, 1.0 }
-- local result = linear_rgb(normalized_srgb)
-- local r = result[1]
-- local g = result[2]
-- local b = result[3]
local function linear_rgb(srgb)
local linear = {}
for i, component in pairs(srgb) do
if component <= 0.04045 then
linear[i] = component / 12.92
else
linear[i] = ((component + 0.055) / 1.055) ^ 2.4
end
end
return linear
end
--- Calculates the relative luminance of a linear RGB color.
-- @tparam {float,float,float} linear_rgb A linear RGB color.
-- @treturn float The relative luminance of the given color.
-- @usage
-- local linear_rgb = {1.0, 1.0, 1.0}
-- local relative_luminance = luminance(linear_rgb)
local function luminance(linear_rgb)
local r, g, b = table.unpack(linear_rgb)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
end
--- Calculates the relative luminance of a hexedecimal sRGB color.
-- @tparam string hex An sRGB color in the hexadecimal format #RRGGBB.
-- @treturn float The relative luminance of the given color.
-- @usage local relative_luminance = luminance_from_hex_srgb("#FFFFFF")
local function luminance_from_hex_srgb(hex)
local srgb = srgb(hex)
local linear_rgb = linear_rgb(srgb)
return luminance(linear_rgb)
end
--- Calculates the relative luminance of the middle gray (#808080).
-- @treturn float The relative luminance of the middle gray (#808080).
local function middle_gray()
local srgb = {0.5, 0.5, 0.5}
local linear_rgb = linear_rgb(srgb)
return luminance(linear_rgb)
end
--- Runs the demonstration.
local function demo()
local color_schemes = require("colors")
local l2 = middle_gray()
for name, colors in pairs(color_schemes) do
local l1 = luminance_from_hex_srgb(colors.base00)
if l1 < l2 then
print(string.format("%s is dark", name))
else
print(string.format("%s is light", name))
end
end
end
demo() Here is a manually summarized table of the results. Table
|
@mamekoro I'm open to PRs. |
It would be great if themes in this repo would also set
vim.o.background
, which is used by some plugins.For example, https://github.com/nvim-tree/nvim-web-devicons, use this variable to select different colors for icons
The text was updated successfully, but these errors were encountered: