Skip to content

Commit 61c62e4

Browse files
authored
feat: Add HSV as accepted color format (#610)
* Add HSV as accepted color format * Update HSV format and support lettercase correct * Fix formatting of color string * Update color format
1 parent 4bfb6a1 commit 61c62e4

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

packages/uui-color-picker/lib/uui-color-picker.element.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ interface EyeDropperInterface {
4646
open: () => Promise<{ sRGBHex: string }>;
4747
}
4848

49-
export type UUIColorPickerFormat = 'hex' | 'rgb' | 'hsl';
49+
export type UUIColorPickerFormat = 'hex' | 'rgb' | 'hsl' | 'hsv';
5050
export type UUIColorPickerFormatWithAlpha =
5151
| UUIColorPickerFormat
5252
| 'hexa'
5353
| 'rgba'
54-
| 'hsla';
54+
| 'hsla'
55+
| 'hsva';
5556

5657
declare const EyeDropper: EyeDropperConstructor;
5758

@@ -85,7 +86,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
8586
@property() value = '';
8687

8788
/**
88-
* The format to use for the display value. If opacity is enabled, these will translate to HEXA, RGBA, and HSLA
89+
* The format to use for the display value. If opacity is enabled, these will translate to HEXA, RGBA, HSLA, and HSVA
8990
* respectively. The color picker will always accept user input in any format (including CSS color names) and convert
9091
* it to the desired format.
9192
* @attr
@@ -153,7 +154,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
153154

154155
/**
155156
* An array of predefined color swatches to display. Can include any format the color picker can parse, including
156-
* HEX(A), RGB(A), HSL(A), and CSS color names.
157+
* HEX(A), RGB(A), HSL(A), HSV(A), and CSS color names.
157158
*/
158159
@property({ attribute: false }) swatches: string[] = [
159160
'#d0021b',
@@ -198,26 +199,31 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
198199
/** Returns the current value as a string in the specified format. */
199200
getFormattedValue(format: UUIColorPickerFormat) {
200201
const formatToUse = this.opacity ? `${format}a` : format;
201-
const { h, l, s } = this._colord.toHsl();
202+
const hexa = this._colord.toHex();
203+
const hex = hexa.length > 7 ? hexa.substring(0, hexa.length - 2) : hexa;
204+
202205
const { r, g, b } = this._colord.toRgb();
203-
const hexa = this.setLetterCase(this._colord.toHex());
204-
const hex = this.setLetterCase(
205-
hexa.length > 7 ? hexa.substring(0, hexa.length - 2) : hexa
206-
);
206+
const { h, s, l } = this._colord.toHsl();
207+
const { v } = this._colord.toHsv();
208+
const a = this._colord.alpha();
207209

208210
switch (formatToUse) {
209211
case 'hex':
210-
return hex;
212+
return this.setLetterCase(hex);
211213
case 'hexa':
212-
return hexa;
214+
return this.setLetterCase(hexa);
213215
case 'rgb':
214-
return `rgb(${r} ${g} ${b})`;
216+
return this.setLetterCase(`rgb(${r}, ${g}, ${b})`);
215217
case 'rgba':
216-
return this._colord.toRgbString();
218+
return this.setLetterCase(this._colord.toRgbString());
217219
case 'hsl':
218-
return `hsl(${h} ${s} ${l})`;
220+
return this.setLetterCase(`hsl(${h}, ${s}%, ${l}%)`);
219221
case 'hsla':
220-
return this._colord.toHslString();
222+
return this.setLetterCase(this._colord.toHslString());
223+
case 'hsv':
224+
return this.setLetterCase(`hsv(${h}, ${s}%, ${l}%)`);
225+
case 'hsva':
226+
return this.setLetterCase(`hsva(${h}, ${s}%, ${v}%, ${a})`); //this._colord.toHsvString();
221227
default:
222228
return '';
223229
}
@@ -236,9 +242,9 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
236242
}
237243

238244
handleFormatToggle() {
239-
const formats = ['hex', 'rgb', 'hsl'];
245+
const formats = ['hex', 'rgb', 'hsl', 'hsv'];
240246
const nextIndex = (formats.indexOf(this.format) + 1) % formats.length;
241-
this.format = formats[nextIndex] as 'hex' | 'rgb' | 'hsl';
247+
this.format = formats[nextIndex] as 'hex' | 'rgb' | 'hsl' | 'hsv';
242248
this._syncValues();
243249
}
244250

0 commit comments

Comments
 (0)