local M = {} local GOLDEN_RATIO_CONJUGATE = 0.618033988749895 --- This is totally not AI generated and I definitely know what this all means, trust me bro! ---@param str string ---@return number local function hash(str) local h = 2166136261 for i = 1, #str do h = bit.bxor(h, string.byte(str, i)) h = (h * 16777619) % 2 ^ 32 end return h end ---@param hue number ---@param sat number ---@param light number ---@return string local function hsl_to_hex(hue, sat, light) local h = hue / 60 local c = (1 - math.abs(2 * light - 1)) * sat local x = c * (1 - math.abs(h % 2 - 1)) local m = light - c / 2 local r, g, b if h < 1 then r, g, b = c, x, 0 elseif h < 2 then r, g, b = x, c, 0 elseif h < 3 then r, g, b = 0, c, x elseif h < 4 then r, g, b = 0, x, c elseif h < 5 then r, g, b = x, 0, c else r, g, b = c, 0, x end return string.format( "#%02x%02x%02x", math.floor((r + m) * 255), math.floor((g + m) * 255), math.floor((b + m) * 255) ) end --- Returns a stable "golden ratio" color from a given string, in hex format. ---@param str string ---@param sat number ---@param light number ---@return string function M.stable_from_string(str, sat, light) local hue = ((hash(str) * GOLDEN_RATIO_CONJUGATE) % 1) * 360 return hsl_to_hex(hue, sat, light) end ---@type jjannotate.Config.ResolvedPalette local palette_dark = { fg = { sat = 0.6, light = 0.7 }, fg_bold = { sat = 0.75, light = 0.75 }, bg = { sat = 0.25, light = 0.125 }, } ---@type jjannotate.Config.ResolvedPalette local palette_light = { fg = { sat = 0.35, light = 0.35 }, fg_bold = { sat = 0.5, light = 0.3 }, bg = { sat = 0.25, light = 0.925 }, } M.palette_dark = palette_dark M.palette_light = palette_light ---@param x number ---@param min number ---@param max number ---@return number local function clamp(x, min, max) return math.min(math.max(x, min), max) end ---@param base jjannotate.Config.ResolvedPalette ---@param multiplier jjannotate.Config.Palette.Color ---@return jjannotate.Config.ResolvedPalette local function apply_multiplier(base, multiplier) return vim.tbl_map( function(color) return { sat = clamp(color.sat * multiplier.sat, 0, 1), light = clamp(color.light * multiplier.light, 0, 1), } end, base ) --[[@as jjannotate.Config.ResolvedPalette]] end ---@param palette jjannotate.Config.Palette ---@return jjannotate.Config.ResolvedPalette function M.resolve_palette(palette) if palette == "auto" then return vim.o.background == "dark" and palette_dark or palette_light end if palette.auto then local base = vim.o.background == "dark" and palette_dark or palette_light local m = palette.multiplier or { sat = 1, light = 1 } return apply_multiplier(base, m) end return palette --[[@as jjannotate.Config.ResolvedPalette]] end return M