Move parsing of colors fully into C

Uses a perfect hash function for color name lookup
This commit is contained in:
Kovid Goyal
2025-12-31 14:19:10 +05:30
parent e14e34948e
commit 573bfb688a
11 changed files with 4709 additions and 790 deletions

View File

@@ -65,25 +65,22 @@ class TestDataTypes(BaseTest):
def c(spec, r=0, g=0, b=0, a=0):
c = to_color(spec)
self.ae(c.red, r, spec)
self.ae(c.green, g, spec)
self.ae(c.blue, b, spec)
self.ae(c.alpha, a, spec)
self.ae(Color(r, g, b, a), c, spec)
c('#eee # comment', 0xee, 0xee, 0xee)
c('#234567', 0x23, 0x45, 0x67)
c('#abcabcdef', 0xab, 0xab, 0xde)
c('#abCabcdef', 0xab, 0xab, 0xde)
c('rgb:e/e/e # comment', 0xee, 0xee, 0xee)
c('rgb:23/45/67', 0x23, 0x45, 0x67)
c('rgB:23/45/67', 0x23, 0x45, 0x67)
c('rgb:abc/abc/def', 0xab, 0xab, 0xde)
c('red', 0xff, 0, 0)
c('alice blue # comment', 240, 248, 255)
c('rEd', 0xff, 0, 0)
c('aLice blUe # comment', 240, 248, 255)
c('oklch(1,0,0)', 255, 255, 255)
c('oklch(0,0,0)', 0, 0, 0)
c('oklch(0.5,0.1,180)', 0, 117, 101)
c('oklch(0.7 0.15 140) # comment', 0x68, 0xb4, 0x57)
c('oklcH(0.7 0.15 140) # comment', 0x68, 0xb4, 0x57)
c('oklch(0.9 0.05 265)', 0xce, 0xde, 0xff)
c('lab(70 50 -30)', 0xea, 0x88, 0xe2)
c('lAb(70 50 -30)', 0xea, 0x88, 0xe2)
c('lab(50,0,0)', 199, 199, 199)
c('lab(100,0,0)', 255, 255, 255)
c('lab(0,0,0)', 0, 0, 0)

View File

@@ -4,7 +4,6 @@
from kitty.config import defaults
from kitty.fast_data_types import DECAWM, DECCOLM, DECOM, IRM, VT_PARSER_BUFFER_SIZE, Color, ColorProfile, Cursor
from kitty.marks import marker_from_function, marker_from_regex
from kitty.rgb import color_names
from kitty.window import pagerhist
from . import BaseTest, draw_multicell, parse_bytes
@@ -1578,7 +1577,8 @@ class TestScreen(BaseTest):
q({k: '?' for k in 'background foreground 213 unknown'.split()}, {
'background': defaults.background, 'foreground': defaults.foreground, '213': defaults.color213, 'unknown': '?'})
q({'background':'aquamarine'})
q({'background':'?', 'selection_background': '?'}, {'background': color_names['aquamarine'], 'selection_background': s.color_profile.highlight_bg})
q({'background':'?', 'selection_background': '?'}, {
'background': Color.parse_color('Aquamarine'), 'selection_background': s.color_profile.highlight_bg})
q({'selection_background': ''})
self.assertIsNone(s.color_profile.highlight_bg)
q({'selection_background': '?'}, {'selection_background': ''})