-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Theme: optimize seed adjustment #73004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
0eb4d82
6bcda18
70dbc8e
81af116
8986d90
283969f
1760f6f
044f654
609a523
b0af9c7
7f02df8
a20492b
5bed724
912f7cd
8a4c2c7
3474192
9e5cf9a
e017de1
1559ddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,6 @@ import { type TaperChromaOptions, taperChroma } from './taper-chroma'; | |
| * @param target | ||
| * @param direction | ||
| * @param options | ||
| * @param options.strict | ||
| * @param options.lightnessConstraint | ||
| * @param options.lightnessConstraint.type | ||
| * @param options.lightnessConstraint.value | ||
|
|
@@ -42,14 +41,12 @@ export function findColorMeetingRequirements( | |
| { | ||
| lightnessConstraint, | ||
| taperChromaOptions, | ||
| strict = true, | ||
| }: { | ||
| lightnessConstraint?: { | ||
| type: 'force' | 'onlyIfSucceeds'; | ||
| value: number; | ||
| }; | ||
| taperChromaOptions?: TaperChromaOptions; | ||
| strict?: boolean; | ||
| } = {} | ||
| ): { color: ColorTypes; reached: boolean; achieved: number; deficit?: number } { | ||
| // A target of 1 means same color. | ||
|
|
@@ -86,6 +83,11 @@ export function findColorMeetingRequirements( | |
|
|
||
| const contrastWithSeed = getContrast( reference, seed ); | ||
|
|
||
| // Set the boundary based on the direction. | ||
| const mostContrastingL = direction === 'lighter' ? 1 : 0; | ||
| const mostContrastingColor = direction === 'lighter' ? WHITE : BLACK; | ||
| const highestContrast = getContrast( reference, mostContrastingColor ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this block few lines up because we use the |
||
|
|
||
| if ( lightnessConstraint ) { | ||
| // Apply a specific L value. | ||
| // Useful when pinning a step to a specific lightness, of to specify | ||
|
|
@@ -101,36 +103,22 @@ export function findColorMeetingRequirements( | |
| ) { | ||
| return { | ||
| color: colorWithExactL, | ||
| reached: exactLContrast >= target, | ||
| reached: exactLContrast + CONTRAST_EPSILON >= target, | ||
| achieved: exactLContrast, | ||
| deficit: | ||
| exactLContrast >= target | ||
| ? undefined | ||
| : ( target - exactLContrast ) * contrastWithSeed, | ||
| ( exactLContrast >= target | ||
| ? exactLContrast - highestContrast | ||
| : target - exactLContrast ) * contrastWithSeed, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // Set the boundary based on the direction. | ||
| const mostContrastingL = direction === 'lighter' ? 1 : 0; | ||
| const mostContrastingColor = direction === 'lighter' ? WHITE : BLACK; | ||
| const highestContrast = getContrast( reference, mostContrastingColor ); | ||
|
|
||
| // If even the most contrasting color can't reach the target, | ||
| // the target is unreachable. | ||
| if ( highestContrast < target ) { | ||
| if ( strict ) { | ||
| throw new Error( | ||
| `Contrast target ${ target.toFixed( | ||
| 2 | ||
| ) }:1 unreachable in ${ direction } direction` + | ||
| `(boundary achieves ${ highestContrast.toFixed( 3 ) }:1).` | ||
| ); | ||
| } | ||
|
|
||
| // If even the most contrasting color can't reach the target, the target is unreachable. | ||
| // On the othe hand, if the contrast is very close to the target, we consider it reached. | ||
| if ( highestContrast < target + CONTRAST_EPSILON ) { | ||
| return { | ||
| color: mostContrastingColor, | ||
| reached: false, | ||
| reached: highestContrast + CONTRAST_EPSILON >= target, | ||
| achieved: highestContrast, | ||
| deficit: ( target - highestContrast ) * contrastWithSeed, | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,8 +69,9 @@ function calculateRamp( { | |
| keyof Ramp, | ||
| { color: string; warning: boolean } | ||
| >; | ||
| let DEFICIT_DIRECTION: RampDirection = 'lighter'; | ||
| let MAX_DEFICIT = -Infinity; | ||
| let MAX_DEFICIT_DIRECTION: RampDirection = 'lighter'; | ||
| let MAX_DEFICIT_STEP = 'none'; | ||
|
|
||
| // Keep track of the calculated colors, as they are going to be useful | ||
| // when other colors reference them. | ||
|
|
@@ -165,7 +166,6 @@ function calculateRamp( { | |
| adjustedTarget, | ||
| computedDir, | ||
| { | ||
| strict: false, | ||
| lightnessConstraint, | ||
| taperChromaOptions, | ||
| } | ||
|
|
@@ -179,7 +179,8 @@ function calculateRamp( { | |
| searchResults.deficit > MAX_DEFICIT | ||
| ) { | ||
| MAX_DEFICIT = searchResults.deficit; | ||
| DEFICIT_DIRECTION = computedDir; | ||
| MAX_DEFICIT_DIRECTION = computedDir; | ||
| MAX_DEFICIT_STEP = stepName; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -195,8 +196,9 @@ function calculateRamp( { | |
| } | ||
| return { | ||
| rampResults, | ||
| DEFICIT_DIRECTION, | ||
| MAX_DEFICIT, | ||
| MAX_DEFICIT_DIRECTION, | ||
| MAX_DEFICIT_STEP, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -243,7 +245,7 @@ export function buildRamp( | |
| const sortedSteps = sortByDependency( config ); | ||
|
|
||
| // Calculate the ramp with the initial seed. | ||
| const { rampResults, DEFICIT_DIRECTION, MAX_DEFICIT } = calculateRamp( { | ||
| const { rampResults, MAX_DEFICIT, MAX_DEFICIT_DIRECTION } = calculateRamp( { | ||
| seed, | ||
| sortedSteps, | ||
| config, | ||
|
|
@@ -264,7 +266,7 @@ export function buildRamp( | |
|
|
||
| // For a scale with the "lighter" direction, the contrast can be improved | ||
| // by darkening the seed. For "darker" direction, by lightening the seed. | ||
| let betterSeedL = DEFICIT_DIRECTION === 'lighter' ? 0 : 1; | ||
| let betterSeedL = MAX_DEFICIT_DIRECTION === 'lighter' ? 0 : 1; | ||
| let betterDeficit = -MAX_DEFICIT; | ||
| let betterReplaced = false; | ||
|
||
|
|
||
|
|
@@ -296,7 +298,7 @@ export function buildRamp( | |
| // Don't use the `MAX_DEFICIT` value because it's not related to our search, | ||
| // and might even be positive, which would confuse the bisection algorithm. | ||
| bestDeficit = | ||
| iterationResults.DEFICIT_DIRECTION === DEFICIT_DIRECTION | ||
| iterationResults.MAX_DEFICIT_DIRECTION === MAX_DEFICIT_DIRECTION | ||
| ? iterationResults.MAX_DEFICIT | ||
| : -MAX_DEFICIT; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the
strictparam because it was never used. Iftrue, it caused an exception to be thrown if a matching color can't be found. But we don't need that, we return a lot of information about why a match wasn't found:achieved,reached,deficit, ...