Skip to content

Commit 6762e70

Browse files
committed
color bug fix
1 parent d33466b commit 6762e70

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ materialyoucolor/quantize
66
__pycache__
77
*.pyc
88
image.jpg
9+
tests/compare*

materialyoucolor/dynamiccolor/color_spec.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ def get_tone(self, scheme: "DynamicScheme", color: "DynamicColor") -> float:
275275

276276

277277
class ColorCalculationDelegateImpl2025(ColorCalculationDelegate):
278+
@staticmethod
279+
def _is_fixed_dim_name(name: str) -> bool:
280+
lowered = name.lower()
281+
return lowered.endswith("_fixed_dim") or lowered.endswith("fixeddim")
282+
278283
def get_hct(self, scheme: "DynamicScheme", color: "DynamicColor") -> "Hct":
279284
palette = color.palette(scheme)
280285
tone = color.get_tone(scheme)
@@ -345,7 +350,7 @@ def get_tone(self, scheme: "DynamicScheme", color: "DynamicColor") -> float:
345350
else DynamicColor.foreground_tone(bg_tone, self_contrast)
346351
)
347352

348-
if color.is_background and not color.name.endswith("_fixed_dim"):
353+
if color.is_background and not self._is_fixed_dim_name(color.name):
349354
if self_tone >= 57:
350355
self_tone = clamp_double(65, 100, self_tone)
351356
else:
@@ -371,7 +376,7 @@ def get_tone(self, scheme: "DynamicScheme", color: "DynamicColor") -> float:
371376
else DynamicColor.foreground_tone(bg_tone, desired_ratio)
372377
)
373378

374-
if color.is_background and not color.name.endswith("_fixed_dim"):
379+
if color.is_background and not self._is_fixed_dim_name(color.name):
375380
if answer >= 57:
376381
answer = clamp_double(65, 100, answer)
377382
else:

materialyoucolor/dynamiccolor/color_spec_2021.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ def primary(self) -> DynamicColor:
263263
return DynamicColor.from_palette(
264264
name="primary",
265265
palette=lambda s: s.primary_palette,
266-
tone=lambda s: 100 if is_monochrome(s) else (80 if s.is_dark else 40),
266+
tone=lambda s: (
267+
(100 if s.is_dark else 0)
268+
if is_monochrome(s)
269+
else (80 if s.is_dark else 40)
270+
),
267271
is_background=True,
268272
background=lambda s: self.highestSurface(s),
269273
contrast_curve=lambda s: ContrastCurve(3, 4.5, 7, 7),
@@ -279,7 +283,11 @@ def onPrimary(self) -> DynamicColor:
279283
return DynamicColor.from_palette(
280284
name="onPrimary",
281285
palette=lambda s: s.primary_palette,
282-
tone=lambda s: 10 if is_monochrome(s) else (20 if s.is_dark else 100),
286+
tone=lambda s: (
287+
(10 if s.is_dark else 90)
288+
if is_monochrome(s)
289+
else (20 if s.is_dark else 100)
290+
),
283291
background=lambda s: self.primary(),
284292
contrast_curve=lambda s: ContrastCurve(4.5, 7, 11, 21),
285293
)
@@ -291,7 +299,11 @@ def primaryContainer(self) -> DynamicColor:
291299
tone=lambda s: (
292300
s.source_color_hct.tone
293301
if is_fidelity(s)
294-
else (85 if is_monochrome(s) else (30 if s.is_dark else 90))
302+
else (
303+
(85 if s.is_dark else 25)
304+
if is_monochrome(s)
305+
else (30 if s.is_dark else 90)
306+
)
295307
),
296308
is_background=True,
297309
background=lambda s: self.highestSurface(s),
@@ -308,7 +320,11 @@ def onPrimaryContainer(self) -> DynamicColor:
308320
tone=lambda s: (
309321
DynamicColor.foreground_tone(self.primaryContainer().tone(s), 4.5)
310322
if is_fidelity(s)
311-
else (0 if is_monochrome(s) else (90 if s.is_dark else 30))
323+
else (
324+
(0 if s.is_dark else 100)
325+
if is_monochrome(s)
326+
else (90 if s.is_dark else 30)
327+
)
312328
),
313329
background=lambda s: self.primaryContainer(),
314330
contrast_curve=lambda s: ContrastCurve(3, 4.5, 7, 11),
@@ -343,7 +359,11 @@ def onSecondary(self) -> DynamicColor:
343359
return DynamicColor.from_palette(
344360
name="onSecondary",
345361
palette=lambda s: s.secondary_palette,
346-
tone=lambda s: 10 if is_monochrome(s) else (20 if s.is_dark else 100),
362+
tone=lambda s: (
363+
(10 if s.is_dark else 100)
364+
if is_monochrome(s)
365+
else (20 if s.is_dark else 100)
366+
),
347367
background=lambda s: self.secondary(),
348368
contrast_curve=lambda s: ContrastCurve(4.5, 7, 11, 21),
349369
)
@@ -353,7 +373,7 @@ def secondaryContainer(self) -> DynamicColor:
353373
name="secondaryContainer",
354374
palette=lambda s: s.secondary_palette,
355375
tone=lambda s: (
356-
30
376+
(30 if s.is_dark else 85)
357377
if is_monochrome(s)
358378
else (
359379
find_desired_chroma_by_tone(
@@ -379,7 +399,7 @@ def onSecondaryContainer(self) -> DynamicColor:
379399
name="onSecondaryContainer",
380400
palette=lambda s: s.secondary_palette,
381401
tone=lambda s: (
382-
90
402+
(90 if s.is_dark else 10)
383403
if is_monochrome(s)
384404
else (
385405
DynamicColor.foreground_tone(self.secondaryContainer().tone(s), 4.5)
@@ -395,9 +415,11 @@ def tertiary(self) -> DynamicColor:
395415
return DynamicColor.from_palette(
396416
name="tertiary",
397417
palette=lambda s: s.tertiary_palette,
398-
tone=lambda s: 90
399-
if is_monochrome(s)
400-
else (25 if is_monochrome(s) else (80 if s.is_dark else 40)),
418+
tone=lambda s: (
419+
(90 if s.is_dark else 25)
420+
if is_monochrome(s)
421+
else (80 if s.is_dark else 40)
422+
),
401423
is_background=True,
402424
background=lambda s: self.highestSurface(s),
403425
contrast_curve=lambda s: ContrastCurve(3, 4.5, 7, 7),
@@ -413,9 +435,11 @@ def onTertiary(self) -> DynamicColor:
413435
return DynamicColor.from_palette(
414436
name="onTertiary",
415437
palette=lambda s: s.tertiary_palette,
416-
tone=lambda s: 10
417-
if is_monochrome(s)
418-
else (90 if is_monochrome(s) else (20 if s.is_dark else 100)),
438+
tone=lambda s: (
439+
(10 if s.is_dark else 90)
440+
if is_monochrome(s)
441+
else (20 if s.is_dark else 100)
442+
),
419443
background=lambda s: self.tertiary(),
420444
contrast_curve=lambda s: ContrastCurve(4.5, 7, 11, 21),
421445
)
@@ -425,7 +449,7 @@ def tertiaryContainer(self) -> DynamicColor:
425449
name="tertiaryContainer",
426450
palette=lambda s: s.tertiary_palette,
427451
tone=lambda s: (
428-
60
452+
(60 if s.is_dark else 49)
429453
if is_monochrome(s)
430454
else (
431455
DislikeAnalyzer.fix_if_disliked(
@@ -448,7 +472,7 @@ def onTertiaryContainer(self) -> DynamicColor:
448472
name="onTertiaryContainer",
449473
palette=lambda s: s.tertiary_palette,
450474
tone=lambda s: (
451-
0
475+
(0 if s.is_dark else 100)
452476
if is_monochrome(s)
453477
else (
454478
DynamicColor.foreground_tone(self.tertiaryContainer().tone(s), 4.5)
@@ -502,9 +526,11 @@ def onErrorContainer(self) -> DynamicColor:
502526
return DynamicColor.from_palette(
503527
name="onErrorContainer",
504528
palette=lambda s: s.error_palette,
505-
tone=lambda s: 90
506-
if is_monochrome(s)
507-
else (10 if is_monochrome(s) else (90 if s.is_dark else 30)),
529+
tone=lambda s: (
530+
(90 if s.is_dark else 10)
531+
if is_monochrome(s)
532+
else (90 if s.is_dark else 30)
533+
),
508534
background=lambda s: self.errorContainer(),
509535
contrast_curve=lambda s: ContrastCurve(3, 4.5, 7, 11),
510536
)

materialyoucolor/dynamiccolor/dynamic_color.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def __init__(self, *args, **kwargs):
9898

9999
@classmethod
100100
def from_palette(cls, *args, **kwargs) -> "DynamicColor":
101+
if "tone" not in kwargs or kwargs.get("tone") is None:
102+
kwargs["tone"] = cls.get_initial_tone_from_background(
103+
kwargs.get("background")
104+
)
101105
return cls(*args, **kwargs)
102106

103107
@staticmethod

0 commit comments

Comments
 (0)