-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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
feat(color): support analogous colors to prevent color conflict #19325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import tinycolor from 'tinycolor2'; | ||
|
||
const rgbRegex = /^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/; | ||
export function getContrastingColor(color: string, thresholds = 186) { | ||
|
@@ -51,3 +52,24 @@ export function getContrastingColor(color: string, thresholds = 186) { | |
|
||
return r * 0.299 + g * 0.587 + b * 0.114 > thresholds ? '#000' : '#FFF'; | ||
} | ||
|
||
export function getAnalogousColors(colors: string[], results: number) { | ||
const generatedColors: string[] = []; | ||
// This is to solve the problem that the first three values generated by tinycolor.analogous | ||
// may have the same or very close colors. | ||
const ext = 3; | ||
const analogousColors = colors.map(color => { | ||
const result = tinycolor(color).analogous(results + ext); | ||
return result.slice(ext); | ||
}); | ||
|
||
// [[A, AA, AAA], [B, BB, BBB]] => [A, B, AA, BB, AAA, BBB] | ||
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. I appreciate the explanation of this interleaving. This is one of those little bits of code where there are 90 ways to do it. We might be able to squeeze some performance here, but I don't think anyone's palette will be big enough to cause a perceivable performance problem anway. |
||
while (analogousColors[analogousColors.length - 1]?.length) { | ||
analogousColors.forEach(colors => { | ||
const color = colors.shift() as tinycolor.Instance; | ||
generatedColors.push(color.toHexString()); | ||
}); | ||
} | ||
|
||
return generatedColors; | ||
} |
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'm curious how we landed on 3 for this?
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.
A comment might be useful for future readers.
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.
Good catch! This is to solve the problem that the first three values generated by
tinycolor.analogous
may have the same or very close colors. You can see my local test:Let me add some comments here.