@@ -30,10 +30,11 @@ local defaults = {
3030--- @type colors
3131colors = vim .deepcopy (defaults )
3232
33- --- Convert hex value to rgb
34- --- @param color string
33+ --- Converts a hex color to an RGB table
34+ --- @param color string #Hex color
35+ --- @return integer[] #RGB table {r, g, b}
3536local function hex2rgb (color )
36- color = string. lower (color )
37+ color = color : lower ()
3738
3839 return {
3940 tonumber (color :sub (2 , 3 ), 16 ),
@@ -42,69 +43,79 @@ local function hex2rgb(color)
4243 }
4344end
4445
45- --- @param fg string #foreground color
46- --- @param bg string #background color
47- --- @param alpha number #number between 0 and 1.
48- --- @source: https://github.com/folke/tokyonight.nvim/blob/main/lua/tokyonight/util.lua#L9-L37
46+ --- Blends two hex colors together based on the alpha value.
47+ ---
48+ --- [View source](https://github.com/folke/tokyonight.nvim/blob/main/lua/tokyonight/util.lua)
49+ --- @param fg string #Foreground hex color
50+ --- @param bg string #Background hex color
51+ --- @param alpha number #Blend factor between 0 (only bg) and 1 (only fg)
52+ --- @return string #Hex color of the resulting blended color
4953local function blend (fg , bg , alpha )
5054 local bg_rgb = hex2rgb (bg )
5155 local fg_rgb = hex2rgb (fg )
5256
53- local blend_channel = function (i )
54- local ret = (alpha * fg_rgb [i ] + ((1 - alpha ) * bg_rgb [i ]))
57+ local r = alpha * fg_rgb [1 ] + (1 - alpha ) * bg_rgb [1 ] + 0.5
58+ local g = alpha * fg_rgb [2 ] + (1 - alpha ) * bg_rgb [2 ] + 0.5
59+ local b = alpha * fg_rgb [3 ] + (1 - alpha ) * bg_rgb [3 ] + 0.5
5560
56- return math.floor (math.min (math.max (0 , ret ), 255 ) + 0.5 )
57- end
58-
59- return (" #%02x%02x%02x" ):format (blend_channel (1 ), blend_channel (2 ), blend_channel (3 ))
61+ return (" #%02x%02x%02x" ):format (r , g , b )
6062end
6163
62- --- @param alpha number #number between 0 and 1
63- function string :darken (alpha )
64- return blend (self , " #000000" , alpha )
64+ --- Darkens the current hex color
65+ --- @param s string
66+ --- @param alpha number #Value between 0 and 1
67+ function string .darken (s , alpha )
68+ return blend (s , " #000000" , alpha )
6569end
6670
67- --- @param alpha number #number between 0 and 1
68- function string :lighten (alpha )
69- return blend (self , " #ffffff" , alpha )
71+ --- Lightens the current hex color
72+ --- @param s string
73+ --- @param alpha number #Value between 0 and 1
74+ function string .lighten (s , alpha )
75+ return blend (s , " #ffffff" , alpha )
7076end
7177
72- --- Get the hex value of a color
73- --- @param name string #name of the color
74- --- @param value any #value of the color
75- --- @return string ? #hex string or `nil` if invalid
76- local function get_hex_value (name , value )
77- local logs = require " one_monokai.logs "
78+ --- Resolve and retrieve the value of a color
79+ --- @param name string #Name of the color
80+ --- @param value string | number #Value of the color
81+ --- @return string | number ? #Hex color or 24-bit RGB value, or default if invalid
82+ local function resolve_color (name , value )
83+ local color_type = type ( value )
7884
79- local type_ok , err = pcall (vim .validate , {
80- [" colors(" .. name .. " )" ] = { value , " string" },
81- })
85+ -- Resolve string first to optimize the common case
86+ if color_type == " string" then
87+ if value :lower () == " none" then
88+ return value
89+ end
8290
83- if not type_ok then
84- logs .error (err )
91+ local rgb = vim .api .nvim_get_color_by_name (value )
8592
86- return defaults [name ]
93+ if rgb ~= - 1 then
94+ return value
95+ end
8796 end
8897
89- if value : lower () == " none " then
98+ if color_type == " number " and value >= 0 and value <= 0xFFFFFF then
9099 return value
91100 end
92101
93- local rgb = vim .api .nvim_get_color_by_name (value )
94-
95- if rgb == - 1 then
96- logs .error (" colors(%s): %q is not a valid color" , name , value )
102+ local logs = require " one_monokai.logs"
103+ local default = defaults [name ]
97104
98- return defaults [name ]
99- end
105+ logs .error (
106+ " colors(%s): expected hex color (#rrggbb) or 24-bit RGB color, got %q. Fallback to %q." ,
107+ name ,
108+ value ,
109+ default
110+ )
100111
101- return ( " #%06x " ): format ( rgb )
112+ return default
102113end
103114
104115local config = require " one_monokai.config"
105116
106- for name , value in pairs (config .colors ) do
107- colors [name ] = get_hex_value (name , value )
117+ for name , value in pairs (config .colors or {} ) do
118+ colors [name ] = resolve_color (name , value )
108119end
109120
110121return colors
0 commit comments