-
Notifications
You must be signed in to change notification settings - Fork 56
Color swatch contrast #1207
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
base: main
Are you sure you want to change the base?
Color swatch contrast #1207
Changes from all commits
70f7062
d898eba
1f126e0
1cca554
a8c9b01
26ffd4b
866ce7a
6aeec78
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 |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import { defineElement } from '@umbraco-ui/uui-base/lib/registration'; | ||
| import { property } from 'lit/decorators.js'; | ||
| import { property, state } from 'lit/decorators.js'; | ||
| import { css, html, LitElement, nothing } from 'lit'; | ||
| import { ref } from 'lit/directives/ref.js'; | ||
| import { ifDefined } from 'lit/directives/if-defined.js'; | ||
| import { iconCheck } from '@umbraco-ui/uui-icon-registry-essential/lib/svgs'; | ||
| import { | ||
| ActiveMixin, | ||
|
|
@@ -23,6 +24,9 @@ | |
| 'label', | ||
| SelectableMixin(ActiveMixin(LitElement)), | ||
| ) { | ||
| @state() | ||
| private _contrast: 'dark' | 'light' | undefined = undefined; | ||
|
|
||
| /** | ||
| * Value of the swatch. This will become the color value if color is left undefined, see the property `color` for more details. | ||
| */ | ||
|
|
@@ -90,6 +94,21 @@ | |
|
|
||
| firstUpdated() { | ||
| this._setAriaAttributes(); | ||
|
|
||
| const color = this.color ?? this.value; | ||
| if (color.startsWith('#')) { | ||
| this._contrast = this.#contrast(color) === 'light' ? 'light' : 'dark'; | ||
| } else if (color.startsWith('rgb')) { | ||
| const [r, g, b, a] = color.match(/[.\d]+/g)?.map(Number) ?? [0, 0, 0]; | ||
| if (a <= 0.5) { | ||
| this._contrast = 'light'; | ||
| } else { | ||
| this._contrast = | ||
| this.#contrast(this.#rgbToHex(r, g, b)) === 'light' | ||
| ? 'light' | ||
| : 'dark'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| willUpdate(changedProperties: Map<string, any>) { | ||
|
|
@@ -118,6 +137,30 @@ | |
| this.selectableTarget = button || this; | ||
| } | ||
|
|
||
| #contrast(hex: string): string { | ||
| const rgb = this.#hexToRgb(hex); | ||
| const o = Math.round((rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000); | ||
|
|
||
| return o <= 180 ? 'dark' : 'light'; | ||
|
||
| } | ||
|
|
||
| #hexToRgb(hex: string): number[] { | ||
| hex = hex.startsWith('#') ? hex.slice(1) : hex; | ||
| if (hex.length === 3) { | ||
| hex = Array.from(hex).reduce((str, x) => str + x + x, ''); // 123 -> 112233 | ||
| } | ||
|
|
||
| const bigint = parseInt(hex, 16); | ||
| const r = (bigint >> 16) & 255; | ||
| const g = (bigint >> 8) & 255; | ||
| const b = bigint & 255; | ||
|
|
||
| return [r, g, b]; | ||
| } | ||
|
|
||
| #rgbToHex = (r: number, g: number, b: number, hash: '#' | '' = ''): string => | ||
| hash + ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0'); | ||
|
|
||
| render() { | ||
| return html` | ||
| <button | ||
|
|
@@ -126,7 +169,12 @@ | |
| aria-label=${this.label} | ||
| ?disabled="${this.disabled}" | ||
| title="${this.label}"> | ||
| <div class="color-swatch color-swatch--transparent-bg"> | ||
| <div | ||
| class="color-swatch color-swatch--transparent-bg ${ifDefined( | ||
| this._contrast, | ||
| ) | ||
| ? `color-swatch--${this._contrast}` | ||
| : ''}"> | ||
| <div | ||
| class="color-swatch__color" | ||
| style="background: var(--uui-swatch-color, ${this.color ?? | ||
|
|
@@ -202,6 +250,7 @@ | |
| margin: 0; | ||
| text-align: left; | ||
| border-radius: 3px; | ||
| border: 1px solid #ccc; | ||
| } | ||
|
|
||
| :host(:not([selectable])) #swatch:focus { | ||
|
|
@@ -243,6 +292,7 @@ | |
| flex-direction: column; | ||
| justify-content: center; | ||
| align-items: center; | ||
| margin: 2px; | ||
| } | ||
|
|
||
| :host([show-label]) .color-swatch { | ||
|
|
@@ -251,8 +301,11 @@ | |
| } | ||
|
|
||
| .color-swatch.color-swatch--transparent-bg { | ||
| background-image: | ||
| linear-gradient(45deg, var(--uui-palette-grey) 25%, transparent 25%), | ||
| background-image: linear-gradient( | ||
|
Check failure on line 304 in packages/uui-color-swatch/lib/uui-color-swatch.element.ts
|
||
| 45deg, | ||
| var(--uui-palette-grey) 25%, | ||
| transparent 25% | ||
| ), | ||
| linear-gradient(45deg, transparent 75%, var(--uui-palette-grey) 75%), | ||
| linear-gradient(45deg, transparent 75%, var(--uui-palette-grey) 75%), | ||
| linear-gradient(45deg, var(--uui-palette-grey) 25%, transparent 25%); | ||
|
|
@@ -286,6 +339,16 @@ | |
| opacity: 0; | ||
| } | ||
|
|
||
| .color-swatch.color-swatch--light .color-swatch__check { | ||
| color: #000 !important; | ||
| filter: none; | ||
| } | ||
|
|
||
| .color-swatch.color-swatch--dark .color-swatch__check { | ||
| color: #fff !important; | ||
| filter: none; | ||
| } | ||
|
|
||
| :host([selected]) .color-swatch__check { | ||
| opacity: 1; | ||
| } | ||
|
|
||
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.
The magic numbers 299, 587, 114, and 1000 should be extracted as named constants to explain they represent the RGB to grayscale conversion coefficients and divisor.