base16-nvim icon indicating copy to clipboard operation
base16-nvim copied to clipboard

Set `vim.o.background`

Open simonmandlik opened this issue 2 years ago • 3 comments

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

simonmandlik avatar Aug 28 '23 14:08 simonmandlik

I'm open to suggestions, but there's no good way to automate vim.o.background for the themes which are sourced from the base-16 repo. A heuristic could work, but it'd be brittle. If you want to open a PR added a heuristic to .setup I'd consider merging it.

RRethy avatar Sep 12 '23 01:09 RRethy

I like the idea of setting vim.o.background.

How about the following method of determining whether a color scheme has a light or dark background?

  1. Get the background color (base00) of a color scheme.
  2. Calculate the relative luminance of base00. Call it l1.
  3. Calculate the relative luminance of the middle gray (i.e. rgb(50% 50% 50%) in the CSS syntax). Call it l2.
  4. If l1 < l2, the color scheme is dark because it is darker than the middle gray, otherwise, it is light.

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. It looks pretty good to me.

Table
colorscheme background color Calculated vim.o.background
3024 #090300 dark
apathy #031a16 dark
apprentice #262626 dark
ashes #1c2023 dark
atelier-cave #19171c dark
atelier-dune #20201d dark
atelier-estuary #22221b dark
atelier-forest #1b1918 dark
atelier-heath #1b181b dark
atelier-lakeside #161b1d dark
atelier-plateau #1b1818 dark
atelier-savanna #171c19 dark
atelier-seaside #131513 dark
atelier-sulphurpool #202746 dark
atlas #002635 dark
ayu-dark #0f1419 dark
ayu-mirage #171b24 dark
bespin #28211c dark
black-metal #000000 dark
black-metal-bathory #000000 dark
black-metal-burzum #000000 dark
black-metal-dark-funeral #000000 dark
black-metal-gorgoroth #000000 dark
black-metal-immortal #000000 dark
black-metal-khold #000000 dark
black-metal-marduk #000000 dark
black-metal-mayhem #000000 dark
black-metal-nile #000000 dark
black-metal-venom #000000 dark
blueforest #141f2e dark
blueish #182430 dark
brewer #0c0d0e dark
bright #000000 dark
brogrammer #1f1f1f dark
brushtrees-dark #485867 dark
catppuccin #1e1e28 dark
catppuccin-frappe #303446 dark
catppuccin-macchiato #24273a dark
catppuccin-mocha #1e1e2e dark
chalk #151515 dark
circus #191919 dark
classic-dark #151515 dark
codeschool #232c31 dark
colors #111111 dark
da-one-black #000000 dark
da-one-gray #181818 dark
da-one-ocean #171726 dark
da-one-sea #22273d dark
danqing #2d302f dark
darcula #2b2b2b dark
darkmoss #171e1f dark
darktooth #1d2021 dark
darkviolet #000000 dark
decaf #2d2d2d dark
default-dark #181818 dark
dracula #282936 dark
edge-dark #262729 dark
eighties #2d2d2d dark
embers #16130f dark
equilibrium-dark #0c1118 dark
equilibrium-gray-dark #111111 dark
espresso #2d2d2d dark
eva #2a3b4d dark
eva-dim #2a3b4d dark
evenok-dark #000000 dark
everforest #2f383e dark
flat #2c3e50 dark
framer #181818 dark
gigavolt #202126 dark
google-dark #1d1f21 dark
gotham #0c1014 dark
grayscale-dark #101010 dark
greenscreen #001100 dark
gruber #181818 dark
gruvbox-dark-hard #1d2021 dark
gruvbox-dark-medium #282828 dark
gruvbox-dark-pale #262626 dark
gruvbox-dark-soft #32302f dark
gruvbox-material-dark-hard #202020 dark
gruvbox-material-dark-medium #292828 dark
gruvbox-material-dark-soft #32302f dark
hardcore #212121 dark
harmonic16-dark #0b1c2c dark
heetch #190134 dark
helios #1d2021 dark
hopscotch #322931 dark
horizon-dark #1c1e26 dark
horizon-terminal-dark #1c1e26 dark
humanoid-dark #232629 dark
ia-dark #1a1a1a dark
icy #021012 dark
irblack #000000 dark
isotope #000000 dark
kanagawa #1f1f28 dark
katy #292d3e dark
kimber #222222 dark
lime #1a1a2f dark
macintosh #000000 dark
marrakesh #201602 dark
materia #263238 dark
material #263238 dark
material-darker #212121 dark
material-palenight #292d3e dark
material-vivid #202124 dark
mellow-purple #1e0528 dark
mocha #3b3228 dark
monokai #272822 dark
mountain #0f0f0f dark
nebula #22273b dark
nord #2e3440 dark
nova #3c4c55 dark
ocean #2b303b dark
oceanicnext #1b2b34 dark
onedark #282c34 dark
outrun-dark #00002a dark
pandora #131213 dark
papercolor-dark #1c1c1c dark
paraiso #2f1e2e dark
pasque #271c3a dark
phd #061229 dark
pico #000000 dark
pinky #171517 dark
pop #000000 dark
porple #292c36 dark
primer-dark #010409 dark
primer-dark-dimmed #1c2128 dark
purpledream #100510 dark
qualia #101010 dark
railscasts #2b2b2b dark
rebecca #292a44 dark
rose-pine #191724 dark
rose-pine-moon #232136 dark
sandcastle #282c34 dark
selenized-black #181818 dark
selenized-dark #103c48 dark
seti #151718 dark
shades-of-purple #1e1e3f dark
shadesmear-dark #232323 dark
silk-dark #0e3c46 dark
snazzy #282a36 dark
solarflare #18262f dark
solarized-dark #002b36 dark
spaceduck #16172d dark
spacemacs #1f2022 dark
standardized-dark #222222 dark
stella #2b213c dark
summercamp #1c1810 dark
summerfruit-dark #151515 dark
synth-midnight-dark #050608 dark
tango #2e3436 dark
tender #282828 dark
tokyo-city-dark #171d23 dark
tokyo-city-terminal-dark #171d23 dark
tokyo-night-dark #1a1b26 dark
tokyo-night-storm #24283b dark
tokyo-night-terminal-dark #16161e dark
tokyo-night-terminal-storm #24283b dark
tokyodark #11121d dark
tokyodark-terminal #11121d dark
tomorrow-night #1d1f21 dark
tomorrow-night-eighties #2d2d2d dark
tube #231f20 dark
twilight #1e1e1e dark
unikitty-dark #2e2a31 dark
unikitty-reversible #2e2a31 dark
uwunicorn #241b26 dark
vice #17191e dark
vulcan #041523 dark
windows-10 #0c0c0c dark
windows-95 #000000 dark
windows-highcontrast #000000 dark
windows-nt #000000 dark
woodland #231e18 dark
xcode-dusk #282b35 dark
zenbones #191919 dark
zenburn #383838 dark
atelier-cave-light #efecf4 light
atelier-dune-light #fefbec light
atelier-estuary-light #f4f3ec light
atelier-forest-light #f1efee light
atelier-heath-light #f7f3f7 light
atelier-lakeside-light #ebf8ff light
atelier-plateau-light #f4ecec light
atelier-savanna-light #ecf4ee light
atelier-seaside-light #f4fbf4 light
atelier-sulphurpool-light #f5f7ff light
ayu-light #fafafa light
brushtrees #e3efef light
catppuccin-latte #eff1f5 light
classic-light #f5f5f5 light
cupcake #fbf1f2 light
cupertino #ffffff light
da-one-paper #faf0dc light
da-one-white #ffffff light
danqing-light #fcfefd light
default-light #f8f8f8 light
dirtysea #e0e0e0 light
edge-light #fafafa light
emil #efefef light
equilibrium-gray-light #f1f1f1 light
equilibrium-light #f5f0e7 light
fruit-soda #f1ecf1 light
github #ffffff light
google-light #ffffff light
grayscale-light #f7f7f7 light
gruvbox-light-hard #f9f5d7 light
gruvbox-light-medium #fbf1c7 light
gruvbox-light-soft #f2e5bc light
gruvbox-material-light-hard #f9f5d7 light
gruvbox-material-light-medium #fbf1c7 light
gruvbox-material-light-soft #f2e5bc light
harmonic16-light #f7f9fb light
heetch-light #feffff light
horizon-light #fdf0ed light
horizon-terminal-light #fdf0ed light
humanoid-light #f8f8f2 light
ia-light #f6f6f6 light
material-lighter #fafafa light
mexico-light #f8f8f8 light
one-light #fafafa light
papercolor-light #eeeeee light
primer-light #fafbfc light
rose-pine-dawn #faf4ed light
sagelight #f8f8f8 light
sakura #feedf3 light
selenized-light #fbf3db light
selenized-white #ffffff light
shadesmear-light #dbdbdb light
shapeshifter #f9f9f9 light
silk-light #e9f1ef light
solarflare-light #f5f7fa light
solarized-light #fdf6e3 light
standardized-light #ffffff light
still-alive #f0f0f0 light
summerfruit-light #ffffff light
synth-midnight-light #dddfe0 light
tokyo-city-light #fbfbfd light
tokyo-city-terminal-light #fbfbfd light
tokyo-night-light #d5d6db light
tokyo-night-terminal-light #d5d6db light
tomorrow #ffffff light
unikitty-light #ffffff light
windows-10-light #f2f2f2 light
windows-95-light #fcfcfc light
windows-highcontrast-light #fcfcfc light
windows-nt-light #ffffff light

mamekoro avatar Apr 07 '24 11:04 mamekoro

@mamekoro I'm open to PRs.

RRethy avatar May 17 '24 15:05 RRethy