File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed
Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @gitbook/colors " : minor
3+ ---
4+
5+ Override tint lightness if supplied color is out of bounds
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments