Skip to content

Commit 4f0a772

Browse files
authored
Override tint lightness if supplied color is out of bounds (#2983)
1 parent 903f962 commit 4f0a772

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

.changeset/large-toys-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@gitbook/colors": minor
3+
---
4+
5+
Override tint lightness if supplied color is out of bounds

packages/colors/src/transformations.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export function colorScale(
184184
const mixColor = mix?.color ? rgbToOklch(hexToRgbArray(mix.color)) : null;
185185
const foregroundColor = rgbToOklch(hexToRgbArray(foreground));
186186
const backgroundColor = rgbToOklch(hexToRgbArray(background));
187+
let mapping = darkMode ? colorMixMapping.dark : colorMixMapping.light;
187188

188189
if (mixColor && mix?.ratio && mix.ratio > 0) {
189190
// If defined, we mix in a (tiny) bit of the mix color with the base color.
@@ -192,7 +193,20 @@ export function colorScale(
192193
baseColor.H = mix.color === DEFAULT_TINT_COLOR ? baseColor.H : mixColor.H;
193194
}
194195

195-
const mapping = darkMode ? colorMixMapping.dark : colorMixMapping.light;
196+
if (
197+
(darkMode && baseColor.L < backgroundColor.L) ||
198+
(!darkMode && baseColor.L > backgroundColor.L)
199+
) {
200+
// If the supplied color is outside of our lightness bounds, use the supplied color's lightness.
201+
// This is mostly used to allow darker-than-dark backgrounds for brands that specifically want that look.
202+
const difference = (backgroundColor.L - baseColor.L) / backgroundColor.L;
203+
backgroundColor.L = baseColor.L;
204+
// At the edges of the scale, the subtle lightness changes stop being perceptible. We need to amp up our mapping to still stand out.
205+
const amplifier = 1;
206+
mapping = mapping.map((step, index) =>
207+
index < 9 ? step + step * amplifier * difference : step
208+
);
209+
}
196210

197211
const result = [];
198212

0 commit comments

Comments
 (0)