Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Theme: use unadjusted seedL for bisection
  • Loading branch information
jsnajdr committed Nov 7, 2025
commit 6bcda187552787fa4adaeaade81ff821057bb91e
13 changes: 5 additions & 8 deletions packages/theme/src/color-ramps/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,9 @@ export function buildRamp(
Math.abs( betterSeedL - worseSeedL ) > LIGHTNESS_EPSILON;
i++
) {
const newSeedL = ( worseSeedL + betterSeedL ) / 2;
const newSeed = clampToGamut(
set(
clone( seed ),
[ OKLCH, 'l' ],
( worseSeedL + betterSeedL ) / 2
)
set( clone( seed ), [ OKLCH, 'l' ], newSeedL )
);

const iterationResults = calculateRamp( {
Expand All @@ -307,17 +304,17 @@ export function buildRamp(
} );

if ( iterationResults.SATISFIED_ALL_CONTRAST_REQUIREMENTS ) {
betterSeedL = get( newSeed, [ OKLCH, 'l' ] );
betterSeedL = newSeedL;
// Only update toReturn when the ramp satisfies all constraints.
toReturn.ramp = iterationResults.rampResults;
} else if ( UNSATISFIED_DIRECTION !== mainDir ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mreishus recently pointed out that this comparison might be wrong. It's true, we want to compare:

iterationResults.MAX_DEFICIT_DIRECTION === MAX_DEFICIT_DIRECTION

The MAX_DEFICIT_DIRECTION (formerly UNSATISFIED_DIRECTION) value specifies in which direction did the first iteration fail to satisfy contrast constraint. And we want to lighten/darken the seed a little bit. But when doing the bisection, it can happen that whe move the seed too much in the opposite direction, and now it can't find contrastful colors in the opposite direction.

In that case, we want to ignore the deficit value because it's not related to our calculation. It's better hardcoded to -MAX_DEFICIT.

// Failing constraint is in opposite direction to main ramp direction
// We've moved too far in mainDir, constrain the search
betterSeedL = get( newSeed, [ OKLCH, 'l' ] );
betterSeedL = newSeedL;
} else {
// Failing constraint is in same direction as main ramp direction
// We haven't moved far enough in mainDir, continue searching
worseSeedL = get( newSeed, [ OKLCH, 'l' ] );
worseSeedL = newSeedL;
}
}
}
Expand Down