mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-24 09:18:08 +02:00
Micro-optimisation
This commit is contained in:
34
kitty/rgb.py
generated
34
kitty/rgb.py
generated
@@ -455,30 +455,28 @@ def to_color(raw: str, validate: bool = False) -> Color | None:
|
|||||||
if raw.startswith('#'):
|
if raw.startswith('#'):
|
||||||
# For hex colors, only strip comments after whitespace
|
# For hex colors, only strip comments after whitespace
|
||||||
# e.g., "#ff0000 # comment" -> "#ff0000"
|
# e.g., "#ff0000 # comment" -> "#ff0000"
|
||||||
parts = raw.split()
|
raw = raw.partition(' ')[0]
|
||||||
if len(parts) > 1:
|
|
||||||
raw = parts[0] # Keep only the hex color part
|
|
||||||
else:
|
else:
|
||||||
# For non-hex colors, strip everything after #
|
# For non-hex colors, strip everything after #
|
||||||
raw = raw.split('#')[0].strip()
|
raw = raw.partition('#')[0]
|
||||||
x = raw.strip().lower()
|
x = raw.strip().lower()
|
||||||
ans = color_names.get(x)
|
if ans := color_names.get(x):
|
||||||
if ans is not None:
|
|
||||||
return ans
|
return ans
|
||||||
val: Color | None = None
|
val: Color | None = None
|
||||||
with suppress(Exception):
|
with suppress(Exception):
|
||||||
if raw.startswith('#'):
|
match raw[0]:
|
||||||
val = parse_sharp(raw[1:])
|
case '#':
|
||||||
elif x.startswith('oklch('):
|
val = parse_sharp(raw[1:])
|
||||||
val = parse_oklch(x[6:])
|
case 'o':
|
||||||
elif x.startswith('lab('):
|
val = parse_oklch(x[6:])
|
||||||
val = parse_lab(x[4:])
|
case 'l':
|
||||||
else:
|
val = parse_lab(x[4:])
|
||||||
k, sep, v = raw.partition(':')
|
case 'r':
|
||||||
if k == 'rgb':
|
k, _, v = raw.partition(':')
|
||||||
val = parse_rgb(v)
|
if k == 'rgb':
|
||||||
elif k == 'rgbi':
|
val = parse_rgb(v)
|
||||||
val = parse_rgbi(v)
|
elif k == 'rgbi':
|
||||||
|
val = parse_rgbi(v)
|
||||||
if val is None and validate:
|
if val is None and validate:
|
||||||
raise ValueError(f'Invalid color name: {raw!r}')
|
raise ValueError(f'Invalid color name: {raw!r}')
|
||||||
return val
|
return val
|
||||||
|
|||||||
@@ -105,9 +105,9 @@ func oklchToSrgbGamutMap(l, c, h float64) (float64, float64, float64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Constants from CSS Color Module Level 4
|
// Constants from CSS Color Module Level 4
|
||||||
const jnd = 0.02 // Just Noticeable Difference threshold
|
const jnd = 0.02 // Just Noticeable Difference threshold
|
||||||
const minConvergence = 0.0001 // Binary search precision
|
const minConvergence = 0.0001 // Binary search precision
|
||||||
const epsilon = 0.00001 // Small value for floating point comparisons
|
const epsilon = 0.00001 // Small value for floating point comparisons
|
||||||
|
|
||||||
// Edge cases: pure black or white
|
// Edge cases: pure black or white
|
||||||
if l <= 0.0 {
|
if l <= 0.0 {
|
||||||
@@ -271,7 +271,7 @@ func parseOklch(spec string) (RGBA, error) {
|
|||||||
// Clamp to reasonable ranges
|
// Clamp to reasonable ranges
|
||||||
l = math.Max(0.0, math.Min(1.0, l))
|
l = math.Max(0.0, math.Min(1.0, l))
|
||||||
c = math.Max(0.0, c) // Chroma is unbounded
|
c = math.Max(0.0, c) // Chroma is unbounded
|
||||||
h = math.Mod(h, 360) // Wrap hue to 0-360
|
h = math.Mod(h, 360) // Wrap hue to 0-360
|
||||||
if h < 0 {
|
if h < 0 {
|
||||||
h += 360
|
h += 360
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user