@sanity/color-input 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.esm.js CHANGED
@@ -1,2 +1,1715 @@
1
- var e,l;function n(e,l){return l||(l=e.slice(0)),Object.freeze(Object.defineProperties(e,{raw:{value:Object.freeze(l)}}))}import{defineType as t,set as o,setIfMissing as a,unset as r,definePlugin as i}from"sanity";import{jsxs as h,jsx as d,Fragment as s}from"react/jsx-runtime";import{useMemo as c,useCallback as u,useRef as p,useState as b,useEffect as m}from"react";import{debounce as g}from"lodash";import{useTheme as y,Flex as f,Box as v,Card as x,Stack as C,Text as w,Inline as z,Button as S}from"@sanity/ui";import{TrashIcon as j,AddIcon as A}from"@sanity/icons";import{EditableInput as L,Saturation as H,Hue as M,Alpha as O,Checkboard as R}from"react-color/lib/components/common";import{CustomPicker as k}from"react-color";import B from"styled-components";import{isValidHex as N}from"react-color/lib/helpers/color";const T=t({title:"Hue Saturation Lightness",name:"hslaColor",type:"object",fields:[{name:"h",type:"number",title:"Hue"},{name:"s",type:"number",title:"Saturation"},{name:"l",type:"number",title:"Lightness"},{name:"a",type:"number",title:"Alpha"}]}),G=t({title:"Red Green Blue (rgb)",name:"rgbaColor",type:"object",fields:[{name:"r",type:"number",title:"Red"},{name:"g",type:"number",title:"Green"},{name:"b",type:"number",title:"Blue"},{name:"a",type:"number",title:"Alpha"}]}),V=e=>{let{onChange:l,rgb:n,hsl:t,hex:o,disableAlpha:a}=e;var r;const{sanity:i}=y(),s=c((()=>({input:{width:"80%",padding:"4px 10% 3px",border:"none",boxShadow:"inset 0 0 0 1px ".concat(i.color.input.default.enabled.border),color:i.color.input.default.enabled.fg,backgroundColor:i.color.input.default.enabled.bg,fontSize:i.fonts.text.sizes[0].fontSize,textAlign:"center"},label:{display:"block",textAlign:"center",fontSize:i.fonts.label.sizes[0].fontSize,color:i.color.base.fg,paddingTop:"3px",paddingBottom:"4px",textTransform:"capitalize"}})),[i]),p=u((e=>{if("hex"in e&&e.hex&&N(e.hex))l({hex:e.hex,source:"hex"});else if(n&&("r"in e&&e.r||"g"in e&&e.g||"b"in e&&e.b))l({r:Number(e.r)||n.r,g:Number(e.g)||n.g,b:Number(e.b)||n.b,a:n.a,source:"rgb"});else if(t&&"a"in e&&e.a){let n=Number(e.a);n<0?n=0:n>100&&(n=100),n/=100,l({h:t.h,s:t.s,l:t.l,a:n,source:"hsl"})}}),[l,t,n]);return h(f,{children:[d(v,{flex:2,marginRight:1,children:d(L,{style:s,label:"hex",value:null==o?void 0:o.replace("#",""),onChange:p})}),d(v,{flex:1,marginRight:1,children:d(L,{style:s,label:"r",value:null==n?void 0:n.r,onChange:p,dragLabel:!0,dragMax:255})}),d(v,{flex:1,marginRight:1,children:d(L,{style:s,label:"g",value:null==n?void 0:n.g,onChange:p,dragLabel:!0,dragMax:255})}),d(v,{flex:1,marginRight:1,children:d(L,{style:s,label:"b",value:null==n?void 0:n.b,onChange:p,dragLabel:!0,dragMax:255})}),!a&&d(v,{flex:1,children:d(L,{style:s,label:"a",value:Math.round(100*(null!=(r=null==n?void 0:n.a)?r:1)),onChange:p,dragLabel:!0,dragMax:100})})]})},_=B(v)(e||(e=n(["\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n"]))),U=B(f)(l||(l=n(["\n margin-top: 6rem;\n background-color: var(--card-bg-color);\n position: relative;\n width: 100%;\n"]))),D=k((e=>{var l,n,t;const{width:o,color:{rgb:a,hex:r,hsv:i,hsl:c},onChange:u,onUnset:p,disableAlpha:b,readOnly:m}=e;return c&&i?d("div",{style:{width:o},children:d(x,{padding:1,border:!0,radius:1,children:h(C,{space:2,children:[!m&&h(s,{children:[d(x,{overflow:"hidden",style:{position:"relative",height:"5em"},children:d(H,{onChange:u,hsl:c,hsv:i})}),d(x,{shadow:1,radius:3,overflow:"hidden",style:{position:"relative",height:"10px"},children:d(M,{hsl:c,onChange:!m&&u})}),!b&&d(x,{shadow:1,radius:3,overflow:"hidden",style:{position:"relative",height:"10px"},children:d(O,{rgb:a,hsl:c,onChange:u})})]}),h(f,{children:[h(x,{flex:1,radius:2,overflow:"hidden",style:{position:"relative",minWidth:"4em"},children:[d(R,{}),d(_,{style:{backgroundColor:"rgba(".concat(null==a?void 0:a.r,",").concat(null==a?void 0:a.g,",").concat(null==a?void 0:a.b,",").concat(null==a?void 0:a.a,")")}}),m&&d(U,{padding:2,paddingBottom:1,sizing:"border",justify:"space-between",children:h(C,{space:3,marginTop:1,children:[d(w,{size:3,weight:"bold",children:r}),h(z,{space:3,children:[h(w,{size:1,children:[d("strong",{children:"RGB: "}),null==a?void 0:a.r," ",null==a?void 0:a.g," ",null==a?void 0:a.b]}),h(w,{size:1,children:[d("strong",{children:"HSL: "})," ",Math.round(null!=(l=null==c?void 0:c.h)?l:0)," ",Math.round(null!=(n=null==c?void 0:c.s)?n:0),"%"," ",Math.round(null!=(t=null==c?void 0:c.l)?t:0)]})]})]})})]}),!m&&h(f,{align:"flex-start",marginLeft:2,children:[d(v,{style:{width:200},children:d(V,{rgb:a,hsl:c,hex:r,onChange:u,disableAlpha:b})}),d(v,{marginLeft:2,children:d(S,{onClick:p,title:"Delete color",icon:j,tone:"critical"})})]})]})]})})}):null})),P={hex:"#24a3e3",hsl:{h:200,s:.7732,l:.5156,a:1},hsv:{h:200,s:.8414,v:.8901,a:1},rgb:{r:46,g:163,b:227,a:1},source:"hex"};function W(e){var l;const{onChange:n,readOnly:t}=e,i=e.value,h=e.schemaType,y=p(null),[f,v]=b(i);m((()=>v(i)),[i]);const x=u((e=>{var l;const t=h.fields.filter((l=>l.name in e)).map((l=>{const n=e[l.name],t="object"===l.type.jsonType;return o(t?Object.assign({_type:l.type.name},n):n,[l.name])}));n([a({_type:h.name}),o(h.name,["_type"]),o(null==(l=e.rgb)?void 0:l.a,["alpha"]),...t])}),[n,h]),C=c((()=>g(x,100)),[x]),w=u((e=>{v(e),C(e)}),[C,v]),z=u((()=>{v(P),x(P)}),[x]),j=u((()=>{v(void 0),n(r())}),[n]);return d(s,{children:i&&i.hex?d(D,{color:f,onChange:w,readOnly:t||"boolean"==typeof h.readOnly&&h.readOnly,disableAlpha:!!(null==(l=h.options)?void 0:l.disableAlpha),onUnset:j}):d(S,{icon:A,mode:"ghost",text:"Create color",ref:y,disabled:Boolean(t),onClick:z})})}const q=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;return Math.round(100*e)},E=t({name:"color",type:"object",title:"Color",components:{input:W},fields:[{title:"Hex",name:"hex",type:"string"},{title:"Alpha",name:"alpha",type:"number"},{title:"Hue Saturation Lightness",name:"hsl",type:"hslaColor"},{title:"Hue Saturation Value",name:"hsv",type:"hsvaColor"},{title:"Red Green Blue (rgb)",name:"rgb",type:"rgbaColor"}],preview:{select:{title:"hex",alpha:"alpha",hex:"hex",hsl:"hsl"},prepare(e){let{title:l,hex:n,hsl:t,alpha:o}=e,a=n||"No color set";return t&&(a="H:".concat(q(t.h)," S:").concat(q(t.s)," L:").concat(q(t.l)," A:").concat(q(o))),{title:l,subtitle:a,media:()=>d("div",{style:{backgroundColor:null!=n?n:"#000",opacity:null!=o?o:1,position:"absolute",height:"100%",width:"100%",top:"0",left:"0"}})}}}}),F=t({title:"Hue Saturation Value",name:"hsvaColor",type:"object",fields:[{name:"h",type:"number",title:"Hue"},{name:"s",type:"number",title:"Saturation"},{name:"v",type:"number",title:"Value"},{name:"a",type:"number",title:"Alpha"}]}),I=i({name:"@sanity/color-input",schema:{types:[T,F,G,E]}});export{W as ColorInput,E as color,I as colorInput,T as hslaColor,F as hsvaColor,G as rgbaColor};
1
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
2
+ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
3
+ import { defineType, set, setIfMissing, unset, definePlugin } from 'sanity';
4
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
5
+ import { useMemo, useCallback, useRef, useState, useEffect } from 'react';
6
+ import { debounce } from 'lodash';
7
+ import { useTheme, Flex, Box, Card, Stack, Text, Inline, Button } from '@sanity/ui';
8
+ import { TrashIcon, AddIcon } from '@sanity/icons';
9
+ import { EditableInput, Saturation, Hue, Alpha, Checkboard } from 'react-color/lib/components/common';
10
+ import { CustomPicker } from 'react-color';
11
+ import styled from 'styled-components';
12
+ import { isValidHex } from 'react-color/lib/helpers/color';
13
+ const hslaColor = defineType({
14
+ title: "Hue Saturation Lightness",
15
+ name: "hslaColor",
16
+ type: "object",
17
+ fields: [{
18
+ name: "h",
19
+ type: "number",
20
+ title: "Hue"
21
+ }, {
22
+ name: "s",
23
+ type: "number",
24
+ title: "Saturation"
25
+ }, {
26
+ name: "l",
27
+ type: "number",
28
+ title: "Lightness"
29
+ }, {
30
+ name: "a",
31
+ type: "number",
32
+ title: "Alpha"
33
+ }]
34
+ });
35
+ const rgbaColor = defineType({
36
+ title: "Red Green Blue (rgb)",
37
+ name: "rgbaColor",
38
+ type: "object",
39
+ fields: [{
40
+ name: "r",
41
+ type: "number",
42
+ title: "Red"
43
+ }, {
44
+ name: "g",
45
+ type: "number",
46
+ title: "Green"
47
+ }, {
48
+ name: "b",
49
+ type: "number",
50
+ title: "Blue"
51
+ }, {
52
+ name: "a",
53
+ type: "number",
54
+ title: "Alpha"
55
+ }]
56
+ });
57
+ const ColorPickerFields = _ref => {
58
+ let {
59
+ onChange,
60
+ rgb,
61
+ hsl,
62
+ hex,
63
+ disableAlpha
64
+ } = _ref;
65
+ var _a;
66
+ const {
67
+ sanity
68
+ } = useTheme();
69
+ const inputStyles = useMemo(() => ({
70
+ input: {
71
+ width: "80%",
72
+ padding: "4px 10% 3px",
73
+ border: "none",
74
+ boxShadow: "inset 0 0 0 1px ".concat(sanity.color.input.default.enabled.border),
75
+ color: sanity.color.input.default.enabled.fg,
76
+ backgroundColor: sanity.color.input.default.enabled.bg,
77
+ fontSize: sanity.fonts.text.sizes[0].fontSize,
78
+ textAlign: "center"
79
+ },
80
+ label: {
81
+ display: "block",
82
+ textAlign: "center",
83
+ fontSize: sanity.fonts.label.sizes[0].fontSize,
84
+ color: sanity.color.base.fg,
85
+ paddingTop: "3px",
86
+ paddingBottom: "4px",
87
+ textTransform: "capitalize"
88
+ }
89
+ }), [sanity]);
90
+ const handleChange = useCallback(data => {
91
+ if ("hex" in data && data.hex && isValidHex(data.hex)) {
92
+ onChange({
93
+ hex: data.hex,
94
+ source: "hex"
95
+ });
96
+ } else if (rgb && ("r" in data && data.r || "g" in data && data.g || "b" in data && data.b)) {
97
+ onChange({
98
+ r: Number(data.r) || rgb.r,
99
+ g: Number(data.g) || rgb.g,
100
+ b: Number(data.b) || rgb.b,
101
+ a: rgb.a,
102
+ source: "rgb"
103
+ });
104
+ } else if (hsl && "a" in data && data.a) {
105
+ let alpha = Number(data.a);
106
+ if (alpha < 0) {
107
+ alpha = 0;
108
+ } else if (alpha > 100) {
109
+ alpha = 100;
110
+ }
111
+ alpha /= 100;
112
+ onChange({
113
+ h: hsl.h,
114
+ s: hsl.s,
115
+ l: hsl.l,
116
+ a: alpha,
117
+ source: "hsl"
118
+ });
119
+ }
120
+ }, [onChange, hsl, rgb]);
121
+ return /* @__PURE__ */jsxs(Flex, {
122
+ children: [/* @__PURE__ */jsx(Box, {
123
+ flex: 2,
124
+ marginRight: 1,
125
+ children: /* @__PURE__ */jsx(EditableInput, {
126
+ style: inputStyles,
127
+ label: "hex",
128
+ value: hex == null ? void 0 : hex.replace("#", ""),
129
+ onChange: handleChange
130
+ })
131
+ }), /* @__PURE__ */jsx(Box, {
132
+ flex: 1,
133
+ marginRight: 1,
134
+ children: /* @__PURE__ */jsx(EditableInput, {
135
+ style: inputStyles,
136
+ label: "r",
137
+ value: rgb == null ? void 0 : rgb.r,
138
+ onChange: handleChange,
139
+ dragLabel: true,
140
+ dragMax: 255
141
+ })
142
+ }), /* @__PURE__ */jsx(Box, {
143
+ flex: 1,
144
+ marginRight: 1,
145
+ children: /* @__PURE__ */jsx(EditableInput, {
146
+ style: inputStyles,
147
+ label: "g",
148
+ value: rgb == null ? void 0 : rgb.g,
149
+ onChange: handleChange,
150
+ dragLabel: true,
151
+ dragMax: 255
152
+ })
153
+ }), /* @__PURE__ */jsx(Box, {
154
+ flex: 1,
155
+ marginRight: 1,
156
+ children: /* @__PURE__ */jsx(EditableInput, {
157
+ style: inputStyles,
158
+ label: "b",
159
+ value: rgb == null ? void 0 : rgb.b,
160
+ onChange: handleChange,
161
+ dragLabel: true,
162
+ dragMax: 255
163
+ })
164
+ }), !disableAlpha && /* @__PURE__ */jsx(Box, {
165
+ flex: 1,
166
+ children: /* @__PURE__ */jsx(EditableInput, {
167
+ style: inputStyles,
168
+ label: "a",
169
+ value: Math.round(((_a = rgb == null ? void 0 : rgb.a) != null ? _a : 1) * 100),
170
+ onChange: handleChange,
171
+ dragLabel: true,
172
+ dragMax: 100
173
+ })
174
+ })]
175
+ });
176
+ };
177
+
178
+ // This file is autogenerated. It's used to publish ESM to npm.
179
+ function _typeof(obj) {
180
+ "@babel/helpers - typeof";
181
+
182
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
183
+ return typeof obj;
184
+ } : function (obj) {
185
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
186
+ }, _typeof(obj);
187
+ }
188
+
189
+ // https://github.com/bgrins/TinyColor
190
+ // Brian Grinstead, MIT License
191
+
192
+ var trimLeft = /^\s+/;
193
+ var trimRight = /\s+$/;
194
+ function tinycolor(color, opts) {
195
+ color = color ? color : "";
196
+ opts = opts || {};
197
+
198
+ // If input is already a tinycolor, return itself
199
+ if (color instanceof tinycolor) {
200
+ return color;
201
+ }
202
+ // If we are called as a function, call using new instead
203
+ if (!(this instanceof tinycolor)) {
204
+ return new tinycolor(color, opts);
205
+ }
206
+ var rgb = inputToRGB(color);
207
+ this._originalInput = color, this._r = rgb.r, this._g = rgb.g, this._b = rgb.b, this._a = rgb.a, this._roundA = Math.round(100 * this._a) / 100, this._format = opts.format || rgb.format;
208
+ this._gradientType = opts.gradientType;
209
+
210
+ // Don't let the range of [0,255] come back in [0,1].
211
+ // Potentially lose a little bit of precision here, but will fix issues where
212
+ // .5 gets interpreted as half of the total, instead of half of 1
213
+ // If it was supposed to be 128, this was already taken care of by `inputToRgb`
214
+ if (this._r < 1) this._r = Math.round(this._r);
215
+ if (this._g < 1) this._g = Math.round(this._g);
216
+ if (this._b < 1) this._b = Math.round(this._b);
217
+ this._ok = rgb.ok;
218
+ }
219
+ tinycolor.prototype = {
220
+ isDark: function isDark() {
221
+ return this.getBrightness() < 128;
222
+ },
223
+ isLight: function isLight() {
224
+ return !this.isDark();
225
+ },
226
+ isValid: function isValid() {
227
+ return this._ok;
228
+ },
229
+ getOriginalInput: function getOriginalInput() {
230
+ return this._originalInput;
231
+ },
232
+ getFormat: function getFormat() {
233
+ return this._format;
234
+ },
235
+ getAlpha: function getAlpha() {
236
+ return this._a;
237
+ },
238
+ getBrightness: function getBrightness() {
239
+ //http://www.w3.org/TR/AERT#color-contrast
240
+ var rgb = this.toRgb();
241
+ return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;
242
+ },
243
+ getLuminance: function getLuminance() {
244
+ //http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
245
+ var rgb = this.toRgb();
246
+ var RsRGB, GsRGB, BsRGB, R, G, B;
247
+ RsRGB = rgb.r / 255;
248
+ GsRGB = rgb.g / 255;
249
+ BsRGB = rgb.b / 255;
250
+ if (RsRGB <= 0.03928) R = RsRGB / 12.92;else R = Math.pow((RsRGB + 0.055) / 1.055, 2.4);
251
+ if (GsRGB <= 0.03928) G = GsRGB / 12.92;else G = Math.pow((GsRGB + 0.055) / 1.055, 2.4);
252
+ if (BsRGB <= 0.03928) B = BsRGB / 12.92;else B = Math.pow((BsRGB + 0.055) / 1.055, 2.4);
253
+ return 0.2126 * R + 0.7152 * G + 0.0722 * B;
254
+ },
255
+ setAlpha: function setAlpha(value) {
256
+ this._a = boundAlpha(value);
257
+ this._roundA = Math.round(100 * this._a) / 100;
258
+ return this;
259
+ },
260
+ toHsv: function toHsv() {
261
+ var hsv = rgbToHsv(this._r, this._g, this._b);
262
+ return {
263
+ h: hsv.h * 360,
264
+ s: hsv.s,
265
+ v: hsv.v,
266
+ a: this._a
267
+ };
268
+ },
269
+ toHsvString: function toHsvString() {
270
+ var hsv = rgbToHsv(this._r, this._g, this._b);
271
+ var h = Math.round(hsv.h * 360),
272
+ s = Math.round(hsv.s * 100),
273
+ v = Math.round(hsv.v * 100);
274
+ return this._a == 1 ? "hsv(" + h + ", " + s + "%, " + v + "%)" : "hsva(" + h + ", " + s + "%, " + v + "%, " + this._roundA + ")";
275
+ },
276
+ toHsl: function toHsl() {
277
+ var hsl = rgbToHsl(this._r, this._g, this._b);
278
+ return {
279
+ h: hsl.h * 360,
280
+ s: hsl.s,
281
+ l: hsl.l,
282
+ a: this._a
283
+ };
284
+ },
285
+ toHslString: function toHslString() {
286
+ var hsl = rgbToHsl(this._r, this._g, this._b);
287
+ var h = Math.round(hsl.h * 360),
288
+ s = Math.round(hsl.s * 100),
289
+ l = Math.round(hsl.l * 100);
290
+ return this._a == 1 ? "hsl(" + h + ", " + s + "%, " + l + "%)" : "hsla(" + h + ", " + s + "%, " + l + "%, " + this._roundA + ")";
291
+ },
292
+ toHex: function toHex(allow3Char) {
293
+ return rgbToHex(this._r, this._g, this._b, allow3Char);
294
+ },
295
+ toHexString: function toHexString(allow3Char) {
296
+ return "#" + this.toHex(allow3Char);
297
+ },
298
+ toHex8: function toHex8(allow4Char) {
299
+ return rgbaToHex(this._r, this._g, this._b, this._a, allow4Char);
300
+ },
301
+ toHex8String: function toHex8String(allow4Char) {
302
+ return "#" + this.toHex8(allow4Char);
303
+ },
304
+ toRgb: function toRgb() {
305
+ return {
306
+ r: Math.round(this._r),
307
+ g: Math.round(this._g),
308
+ b: Math.round(this._b),
309
+ a: this._a
310
+ };
311
+ },
312
+ toRgbString: function toRgbString() {
313
+ return this._a == 1 ? "rgb(" + Math.round(this._r) + ", " + Math.round(this._g) + ", " + Math.round(this._b) + ")" : "rgba(" + Math.round(this._r) + ", " + Math.round(this._g) + ", " + Math.round(this._b) + ", " + this._roundA + ")";
314
+ },
315
+ toPercentageRgb: function toPercentageRgb() {
316
+ return {
317
+ r: Math.round(bound01(this._r, 255) * 100) + "%",
318
+ g: Math.round(bound01(this._g, 255) * 100) + "%",
319
+ b: Math.round(bound01(this._b, 255) * 100) + "%",
320
+ a: this._a
321
+ };
322
+ },
323
+ toPercentageRgbString: function toPercentageRgbString() {
324
+ return this._a == 1 ? "rgb(" + Math.round(bound01(this._r, 255) * 100) + "%, " + Math.round(bound01(this._g, 255) * 100) + "%, " + Math.round(bound01(this._b, 255) * 100) + "%)" : "rgba(" + Math.round(bound01(this._r, 255) * 100) + "%, " + Math.round(bound01(this._g, 255) * 100) + "%, " + Math.round(bound01(this._b, 255) * 100) + "%, " + this._roundA + ")";
325
+ },
326
+ toName: function toName() {
327
+ if (this._a === 0) {
328
+ return "transparent";
329
+ }
330
+ if (this._a < 1) {
331
+ return false;
332
+ }
333
+ return hexNames[rgbToHex(this._r, this._g, this._b, true)] || false;
334
+ },
335
+ toFilter: function toFilter(secondColor) {
336
+ var hex8String = "#" + rgbaToArgbHex(this._r, this._g, this._b, this._a);
337
+ var secondHex8String = hex8String;
338
+ var gradientType = this._gradientType ? "GradientType = 1, " : "";
339
+ if (secondColor) {
340
+ var s = tinycolor(secondColor);
341
+ secondHex8String = "#" + rgbaToArgbHex(s._r, s._g, s._b, s._a);
342
+ }
343
+ return "progid:DXImageTransform.Microsoft.gradient(" + gradientType + "startColorstr=" + hex8String + ",endColorstr=" + secondHex8String + ")";
344
+ },
345
+ toString: function toString(format) {
346
+ var formatSet = !!format;
347
+ format = format || this._format;
348
+ var formattedString = false;
349
+ var hasAlpha = this._a < 1 && this._a >= 0;
350
+ var needsAlphaFormat = !formatSet && hasAlpha && (format === "hex" || format === "hex6" || format === "hex3" || format === "hex4" || format === "hex8" || format === "name");
351
+ if (needsAlphaFormat) {
352
+ // Special case for "transparent", all other non-alpha formats
353
+ // will return rgba when there is transparency.
354
+ if (format === "name" && this._a === 0) {
355
+ return this.toName();
356
+ }
357
+ return this.toRgbString();
358
+ }
359
+ if (format === "rgb") {
360
+ formattedString = this.toRgbString();
361
+ }
362
+ if (format === "prgb") {
363
+ formattedString = this.toPercentageRgbString();
364
+ }
365
+ if (format === "hex" || format === "hex6") {
366
+ formattedString = this.toHexString();
367
+ }
368
+ if (format === "hex3") {
369
+ formattedString = this.toHexString(true);
370
+ }
371
+ if (format === "hex4") {
372
+ formattedString = this.toHex8String(true);
373
+ }
374
+ if (format === "hex8") {
375
+ formattedString = this.toHex8String();
376
+ }
377
+ if (format === "name") {
378
+ formattedString = this.toName();
379
+ }
380
+ if (format === "hsl") {
381
+ formattedString = this.toHslString();
382
+ }
383
+ if (format === "hsv") {
384
+ formattedString = this.toHsvString();
385
+ }
386
+ return formattedString || this.toHexString();
387
+ },
388
+ clone: function clone() {
389
+ return tinycolor(this.toString());
390
+ },
391
+ _applyModification: function _applyModification(fn, args) {
392
+ var color = fn.apply(null, [this].concat([].slice.call(args)));
393
+ this._r = color._r;
394
+ this._g = color._g;
395
+ this._b = color._b;
396
+ this.setAlpha(color._a);
397
+ return this;
398
+ },
399
+ lighten: function lighten() {
400
+ return this._applyModification(_lighten, arguments);
401
+ },
402
+ brighten: function brighten() {
403
+ return this._applyModification(_brighten, arguments);
404
+ },
405
+ darken: function darken() {
406
+ return this._applyModification(_darken, arguments);
407
+ },
408
+ desaturate: function desaturate() {
409
+ return this._applyModification(_desaturate, arguments);
410
+ },
411
+ saturate: function saturate() {
412
+ return this._applyModification(_saturate, arguments);
413
+ },
414
+ greyscale: function greyscale() {
415
+ return this._applyModification(_greyscale, arguments);
416
+ },
417
+ spin: function spin() {
418
+ return this._applyModification(_spin, arguments);
419
+ },
420
+ _applyCombination: function _applyCombination(fn, args) {
421
+ return fn.apply(null, [this].concat([].slice.call(args)));
422
+ },
423
+ analogous: function analogous() {
424
+ return this._applyCombination(_analogous, arguments);
425
+ },
426
+ complement: function complement() {
427
+ return this._applyCombination(_complement, arguments);
428
+ },
429
+ monochromatic: function monochromatic() {
430
+ return this._applyCombination(_monochromatic, arguments);
431
+ },
432
+ splitcomplement: function splitcomplement() {
433
+ return this._applyCombination(_splitcomplement, arguments);
434
+ },
435
+ // Disabled until https://github.com/bgrins/TinyColor/issues/254
436
+ // polyad: function (number) {
437
+ // return this._applyCombination(polyad, [number]);
438
+ // },
439
+ triad: function triad() {
440
+ return this._applyCombination(polyad, [3]);
441
+ },
442
+ tetrad: function tetrad() {
443
+ return this._applyCombination(polyad, [4]);
444
+ }
445
+ };
446
+
447
+ // If input is an object, force 1 into "1.0" to handle ratios properly
448
+ // String input requires "1.0" as input, so 1 will be treated as 1
449
+ tinycolor.fromRatio = function (color, opts) {
450
+ if (_typeof(color) == "object") {
451
+ var newColor = {};
452
+ for (var i in color) {
453
+ if (color.hasOwnProperty(i)) {
454
+ if (i === "a") {
455
+ newColor[i] = color[i];
456
+ } else {
457
+ newColor[i] = convertToPercentage(color[i]);
458
+ }
459
+ }
460
+ }
461
+ color = newColor;
462
+ }
463
+ return tinycolor(color, opts);
464
+ };
465
+
466
+ // Given a string or object, convert that input to RGB
467
+ // Possible string inputs:
468
+ //
469
+ // "red"
470
+ // "#f00" or "f00"
471
+ // "#ff0000" or "ff0000"
472
+ // "#ff000000" or "ff000000"
473
+ // "rgb 255 0 0" or "rgb (255, 0, 0)"
474
+ // "rgb 1.0 0 0" or "rgb (1, 0, 0)"
475
+ // "rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1"
476
+ // "rgba (1.0, 0, 0, 1)" or "rgba 1.0, 0, 0, 1"
477
+ // "hsl(0, 100%, 50%)" or "hsl 0 100% 50%"
478
+ // "hsla(0, 100%, 50%, 1)" or "hsla 0 100% 50%, 1"
479
+ // "hsv(0, 100%, 100%)" or "hsv 0 100% 100%"
480
+ //
481
+ function inputToRGB(color) {
482
+ var rgb = {
483
+ r: 0,
484
+ g: 0,
485
+ b: 0
486
+ };
487
+ var a = 1;
488
+ var s = null;
489
+ var v = null;
490
+ var l = null;
491
+ var ok = false;
492
+ var format = false;
493
+ if (typeof color == "string") {
494
+ color = stringInputToObject(color);
495
+ }
496
+ if (_typeof(color) == "object") {
497
+ if (isValidCSSUnit(color.r) && isValidCSSUnit(color.g) && isValidCSSUnit(color.b)) {
498
+ rgb = rgbToRgb(color.r, color.g, color.b);
499
+ ok = true;
500
+ format = String(color.r).substr(-1) === "%" ? "prgb" : "rgb";
501
+ } else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.v)) {
502
+ s = convertToPercentage(color.s);
503
+ v = convertToPercentage(color.v);
504
+ rgb = hsvToRgb(color.h, s, v);
505
+ ok = true;
506
+ format = "hsv";
507
+ } else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.l)) {
508
+ s = convertToPercentage(color.s);
509
+ l = convertToPercentage(color.l);
510
+ rgb = hslToRgb(color.h, s, l);
511
+ ok = true;
512
+ format = "hsl";
513
+ }
514
+ if (color.hasOwnProperty("a")) {
515
+ a = color.a;
516
+ }
517
+ }
518
+ a = boundAlpha(a);
519
+ return {
520
+ ok: ok,
521
+ format: color.format || format,
522
+ r: Math.min(255, Math.max(rgb.r, 0)),
523
+ g: Math.min(255, Math.max(rgb.g, 0)),
524
+ b: Math.min(255, Math.max(rgb.b, 0)),
525
+ a: a
526
+ };
527
+ }
528
+
529
+ // Conversion Functions
530
+ // --------------------
531
+
532
+ // `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from:
533
+ // <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
534
+
535
+ // `rgbToRgb`
536
+ // Handle bounds / percentage checking to conform to CSS color spec
537
+ // <http://www.w3.org/TR/css3-color/>
538
+ // *Assumes:* r, g, b in [0, 255] or [0, 1]
539
+ // *Returns:* { r, g, b } in [0, 255]
540
+ function rgbToRgb(r, g, b) {
541
+ return {
542
+ r: bound01(r, 255) * 255,
543
+ g: bound01(g, 255) * 255,
544
+ b: bound01(b, 255) * 255
545
+ };
546
+ }
547
+
548
+ // `rgbToHsl`
549
+ // Converts an RGB color value to HSL.
550
+ // *Assumes:* r, g, and b are contained in [0, 255] or [0, 1]
551
+ // *Returns:* { h, s, l } in [0,1]
552
+ function rgbToHsl(r, g, b) {
553
+ r = bound01(r, 255);
554
+ g = bound01(g, 255);
555
+ b = bound01(b, 255);
556
+ var max = Math.max(r, g, b),
557
+ min = Math.min(r, g, b);
558
+ var h,
559
+ s,
560
+ l = (max + min) / 2;
561
+ if (max == min) {
562
+ h = s = 0; // achromatic
563
+ } else {
564
+ var d = max - min;
565
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
566
+ switch (max) {
567
+ case r:
568
+ h = (g - b) / d + (g < b ? 6 : 0);
569
+ break;
570
+ case g:
571
+ h = (b - r) / d + 2;
572
+ break;
573
+ case b:
574
+ h = (r - g) / d + 4;
575
+ break;
576
+ }
577
+ h /= 6;
578
+ }
579
+ return {
580
+ h: h,
581
+ s: s,
582
+ l: l
583
+ };
584
+ }
585
+
586
+ // `hslToRgb`
587
+ // Converts an HSL color value to RGB.
588
+ // *Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100]
589
+ // *Returns:* { r, g, b } in the set [0, 255]
590
+ function hslToRgb(h, s, l) {
591
+ var r, g, b;
592
+ h = bound01(h, 360);
593
+ s = bound01(s, 100);
594
+ l = bound01(l, 100);
595
+ function hue2rgb(p, q, t) {
596
+ if (t < 0) t += 1;
597
+ if (t > 1) t -= 1;
598
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
599
+ if (t < 1 / 2) return q;
600
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
601
+ return p;
602
+ }
603
+ if (s === 0) {
604
+ r = g = b = l; // achromatic
605
+ } else {
606
+ var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
607
+ var p = 2 * l - q;
608
+ r = hue2rgb(p, q, h + 1 / 3);
609
+ g = hue2rgb(p, q, h);
610
+ b = hue2rgb(p, q, h - 1 / 3);
611
+ }
612
+ return {
613
+ r: r * 255,
614
+ g: g * 255,
615
+ b: b * 255
616
+ };
617
+ }
618
+
619
+ // `rgbToHsv`
620
+ // Converts an RGB color value to HSV
621
+ // *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1]
622
+ // *Returns:* { h, s, v } in [0,1]
623
+ function rgbToHsv(r, g, b) {
624
+ r = bound01(r, 255);
625
+ g = bound01(g, 255);
626
+ b = bound01(b, 255);
627
+ var max = Math.max(r, g, b),
628
+ min = Math.min(r, g, b);
629
+ var h,
630
+ s,
631
+ v = max;
632
+ var d = max - min;
633
+ s = max === 0 ? 0 : d / max;
634
+ if (max == min) {
635
+ h = 0; // achromatic
636
+ } else {
637
+ switch (max) {
638
+ case r:
639
+ h = (g - b) / d + (g < b ? 6 : 0);
640
+ break;
641
+ case g:
642
+ h = (b - r) / d + 2;
643
+ break;
644
+ case b:
645
+ h = (r - g) / d + 4;
646
+ break;
647
+ }
648
+ h /= 6;
649
+ }
650
+ return {
651
+ h: h,
652
+ s: s,
653
+ v: v
654
+ };
655
+ }
656
+
657
+ // `hsvToRgb`
658
+ // Converts an HSV color value to RGB.
659
+ // *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100]
660
+ // *Returns:* { r, g, b } in the set [0, 255]
661
+ function hsvToRgb(h, s, v) {
662
+ h = bound01(h, 360) * 6;
663
+ s = bound01(s, 100);
664
+ v = bound01(v, 100);
665
+ var i = Math.floor(h),
666
+ f = h - i,
667
+ p = v * (1 - s),
668
+ q = v * (1 - f * s),
669
+ t = v * (1 - (1 - f) * s),
670
+ mod = i % 6,
671
+ r = [v, q, p, p, t, v][mod],
672
+ g = [t, v, v, q, p, p][mod],
673
+ b = [p, p, t, v, v, q][mod];
674
+ return {
675
+ r: r * 255,
676
+ g: g * 255,
677
+ b: b * 255
678
+ };
679
+ }
680
+
681
+ // `rgbToHex`
682
+ // Converts an RGB color to hex
683
+ // Assumes r, g, and b are contained in the set [0, 255]
684
+ // Returns a 3 or 6 character hex
685
+ function rgbToHex(r, g, b, allow3Char) {
686
+ var hex = [pad2(Math.round(r).toString(16)), pad2(Math.round(g).toString(16)), pad2(Math.round(b).toString(16))];
687
+
688
+ // Return a 3 character hex if possible
689
+ if (allow3Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1)) {
690
+ return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0);
691
+ }
692
+ return hex.join("");
693
+ }
694
+
695
+ // `rgbaToHex`
696
+ // Converts an RGBA color plus alpha transparency to hex
697
+ // Assumes r, g, b are contained in the set [0, 255] and
698
+ // a in [0, 1]. Returns a 4 or 8 character rgba hex
699
+ function rgbaToHex(r, g, b, a, allow4Char) {
700
+ var hex = [pad2(Math.round(r).toString(16)), pad2(Math.round(g).toString(16)), pad2(Math.round(b).toString(16)), pad2(convertDecimalToHex(a))];
701
+
702
+ // Return a 4 character hex if possible
703
+ if (allow4Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1) && hex[3].charAt(0) == hex[3].charAt(1)) {
704
+ return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0) + hex[3].charAt(0);
705
+ }
706
+ return hex.join("");
707
+ }
708
+
709
+ // `rgbaToArgbHex`
710
+ // Converts an RGBA color to an ARGB Hex8 string
711
+ // Rarely used, but required for "toFilter()"
712
+ function rgbaToArgbHex(r, g, b, a) {
713
+ var hex = [pad2(convertDecimalToHex(a)), pad2(Math.round(r).toString(16)), pad2(Math.round(g).toString(16)), pad2(Math.round(b).toString(16))];
714
+ return hex.join("");
715
+ }
716
+
717
+ // `equals`
718
+ // Can be called with any tinycolor input
719
+ tinycolor.equals = function (color1, color2) {
720
+ if (!color1 || !color2) return false;
721
+ return tinycolor(color1).toRgbString() == tinycolor(color2).toRgbString();
722
+ };
723
+ tinycolor.random = function () {
724
+ return tinycolor.fromRatio({
725
+ r: Math.random(),
726
+ g: Math.random(),
727
+ b: Math.random()
728
+ });
729
+ };
730
+
731
+ // Modification Functions
732
+ // ----------------------
733
+ // Thanks to less.js for some of the basics here
734
+ // <https://github.com/cloudhead/less.js/blob/master/lib/less/functions.js>
735
+
736
+ function _desaturate(color, amount) {
737
+ amount = amount === 0 ? 0 : amount || 10;
738
+ var hsl = tinycolor(color).toHsl();
739
+ hsl.s -= amount / 100;
740
+ hsl.s = clamp01(hsl.s);
741
+ return tinycolor(hsl);
742
+ }
743
+ function _saturate(color, amount) {
744
+ amount = amount === 0 ? 0 : amount || 10;
745
+ var hsl = tinycolor(color).toHsl();
746
+ hsl.s += amount / 100;
747
+ hsl.s = clamp01(hsl.s);
748
+ return tinycolor(hsl);
749
+ }
750
+ function _greyscale(color) {
751
+ return tinycolor(color).desaturate(100);
752
+ }
753
+ function _lighten(color, amount) {
754
+ amount = amount === 0 ? 0 : amount || 10;
755
+ var hsl = tinycolor(color).toHsl();
756
+ hsl.l += amount / 100;
757
+ hsl.l = clamp01(hsl.l);
758
+ return tinycolor(hsl);
759
+ }
760
+ function _brighten(color, amount) {
761
+ amount = amount === 0 ? 0 : amount || 10;
762
+ var rgb = tinycolor(color).toRgb();
763
+ rgb.r = Math.max(0, Math.min(255, rgb.r - Math.round(255 * -(amount / 100))));
764
+ rgb.g = Math.max(0, Math.min(255, rgb.g - Math.round(255 * -(amount / 100))));
765
+ rgb.b = Math.max(0, Math.min(255, rgb.b - Math.round(255 * -(amount / 100))));
766
+ return tinycolor(rgb);
767
+ }
768
+ function _darken(color, amount) {
769
+ amount = amount === 0 ? 0 : amount || 10;
770
+ var hsl = tinycolor(color).toHsl();
771
+ hsl.l -= amount / 100;
772
+ hsl.l = clamp01(hsl.l);
773
+ return tinycolor(hsl);
774
+ }
775
+
776
+ // Spin takes a positive or negative amount within [-360, 360] indicating the change of hue.
777
+ // Values outside of this range will be wrapped into this range.
778
+ function _spin(color, amount) {
779
+ var hsl = tinycolor(color).toHsl();
780
+ var hue = (hsl.h + amount) % 360;
781
+ hsl.h = hue < 0 ? 360 + hue : hue;
782
+ return tinycolor(hsl);
783
+ }
784
+
785
+ // Combination Functions
786
+ // ---------------------
787
+ // Thanks to jQuery xColor for some of the ideas behind these
788
+ // <https://github.com/infusion/jQuery-xcolor/blob/master/jquery.xcolor.js>
789
+
790
+ function _complement(color) {
791
+ var hsl = tinycolor(color).toHsl();
792
+ hsl.h = (hsl.h + 180) % 360;
793
+ return tinycolor(hsl);
794
+ }
795
+ function polyad(color, number) {
796
+ if (isNaN(number) || number <= 0) {
797
+ throw new Error("Argument to polyad must be a positive number");
798
+ }
799
+ var hsl = tinycolor(color).toHsl();
800
+ var result = [tinycolor(color)];
801
+ var step = 360 / number;
802
+ for (var i = 1; i < number; i++) {
803
+ result.push(tinycolor({
804
+ h: (hsl.h + i * step) % 360,
805
+ s: hsl.s,
806
+ l: hsl.l
807
+ }));
808
+ }
809
+ return result;
810
+ }
811
+ function _splitcomplement(color) {
812
+ var hsl = tinycolor(color).toHsl();
813
+ var h = hsl.h;
814
+ return [tinycolor(color), tinycolor({
815
+ h: (h + 72) % 360,
816
+ s: hsl.s,
817
+ l: hsl.l
818
+ }), tinycolor({
819
+ h: (h + 216) % 360,
820
+ s: hsl.s,
821
+ l: hsl.l
822
+ })];
823
+ }
824
+ function _analogous(color, results, slices) {
825
+ results = results || 6;
826
+ slices = slices || 30;
827
+ var hsl = tinycolor(color).toHsl();
828
+ var part = 360 / slices;
829
+ var ret = [tinycolor(color)];
830
+ for (hsl.h = (hsl.h - (part * results >> 1) + 720) % 360; --results;) {
831
+ hsl.h = (hsl.h + part) % 360;
832
+ ret.push(tinycolor(hsl));
833
+ }
834
+ return ret;
835
+ }
836
+ function _monochromatic(color, results) {
837
+ results = results || 6;
838
+ var hsv = tinycolor(color).toHsv();
839
+ var h = hsv.h,
840
+ s = hsv.s,
841
+ v = hsv.v;
842
+ var ret = [];
843
+ var modification = 1 / results;
844
+ while (results--) {
845
+ ret.push(tinycolor({
846
+ h: h,
847
+ s: s,
848
+ v: v
849
+ }));
850
+ v = (v + modification) % 1;
851
+ }
852
+ return ret;
853
+ }
854
+
855
+ // Utility Functions
856
+ // ---------------------
857
+
858
+ tinycolor.mix = function (color1, color2, amount) {
859
+ amount = amount === 0 ? 0 : amount || 50;
860
+ var rgb1 = tinycolor(color1).toRgb();
861
+ var rgb2 = tinycolor(color2).toRgb();
862
+ var p = amount / 100;
863
+ var rgba = {
864
+ r: (rgb2.r - rgb1.r) * p + rgb1.r,
865
+ g: (rgb2.g - rgb1.g) * p + rgb1.g,
866
+ b: (rgb2.b - rgb1.b) * p + rgb1.b,
867
+ a: (rgb2.a - rgb1.a) * p + rgb1.a
868
+ };
869
+ return tinycolor(rgba);
870
+ };
871
+
872
+ // Readability Functions
873
+ // ---------------------
874
+ // <http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef (WCAG Version 2)
875
+
876
+ // `contrast`
877
+ // Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)
878
+ tinycolor.readability = function (color1, color2) {
879
+ var c1 = tinycolor(color1);
880
+ var c2 = tinycolor(color2);
881
+ return (Math.max(c1.getLuminance(), c2.getLuminance()) + 0.05) / (Math.min(c1.getLuminance(), c2.getLuminance()) + 0.05);
882
+ };
883
+
884
+ // `isReadable`
885
+ // Ensure that foreground and background color combinations meet WCAG2 guidelines.
886
+ // The third argument is an optional Object.
887
+ // the 'level' property states 'AA' or 'AAA' - if missing or invalid, it defaults to 'AA';
888
+ // the 'size' property states 'large' or 'small' - if missing or invalid, it defaults to 'small'.
889
+ // If the entire object is absent, isReadable defaults to {level:"AA",size:"small"}.
890
+
891
+ // *Example*
892
+ // tinycolor.isReadable("#000", "#111") => false
893
+ // tinycolor.isReadable("#000", "#111",{level:"AA",size:"large"}) => false
894
+ tinycolor.isReadable = function (color1, color2, wcag2) {
895
+ var readability = tinycolor.readability(color1, color2);
896
+ var wcag2Parms, out;
897
+ out = false;
898
+ wcag2Parms = validateWCAG2Parms(wcag2);
899
+ switch (wcag2Parms.level + wcag2Parms.size) {
900
+ case "AAsmall":
901
+ case "AAAlarge":
902
+ out = readability >= 4.5;
903
+ break;
904
+ case "AAlarge":
905
+ out = readability >= 3;
906
+ break;
907
+ case "AAAsmall":
908
+ out = readability >= 7;
909
+ break;
910
+ }
911
+ return out;
912
+ };
913
+
914
+ // `mostReadable`
915
+ // Given a base color and a list of possible foreground or background
916
+ // colors for that base, returns the most readable color.
917
+ // Optionally returns Black or White if the most readable color is unreadable.
918
+ // *Example*
919
+ // tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(); // "#112255"
920
+ // tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString(); // "#ffffff"
921
+ // tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(); // "#faf3f3"
922
+ // tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(); // "#ffffff"
923
+ tinycolor.mostReadable = function (baseColor, colorList, args) {
924
+ var bestColor = null;
925
+ var bestScore = 0;
926
+ var readability;
927
+ var includeFallbackColors, level, size;
928
+ args = args || {};
929
+ includeFallbackColors = args.includeFallbackColors;
930
+ level = args.level;
931
+ size = args.size;
932
+ for (var i = 0; i < colorList.length; i++) {
933
+ readability = tinycolor.readability(baseColor, colorList[i]);
934
+ if (readability > bestScore) {
935
+ bestScore = readability;
936
+ bestColor = tinycolor(colorList[i]);
937
+ }
938
+ }
939
+ if (tinycolor.isReadable(baseColor, bestColor, {
940
+ level: level,
941
+ size: size
942
+ }) || !includeFallbackColors) {
943
+ return bestColor;
944
+ } else {
945
+ args.includeFallbackColors = false;
946
+ return tinycolor.mostReadable(baseColor, ["#fff", "#000"], args);
947
+ }
948
+ };
949
+
950
+ // Big List of Colors
951
+ // ------------------
952
+ // <https://www.w3.org/TR/css-color-4/#named-colors>
953
+ var names = tinycolor.names = {
954
+ aliceblue: "f0f8ff",
955
+ antiquewhite: "faebd7",
956
+ aqua: "0ff",
957
+ aquamarine: "7fffd4",
958
+ azure: "f0ffff",
959
+ beige: "f5f5dc",
960
+ bisque: "ffe4c4",
961
+ black: "000",
962
+ blanchedalmond: "ffebcd",
963
+ blue: "00f",
964
+ blueviolet: "8a2be2",
965
+ brown: "a52a2a",
966
+ burlywood: "deb887",
967
+ burntsienna: "ea7e5d",
968
+ cadetblue: "5f9ea0",
969
+ chartreuse: "7fff00",
970
+ chocolate: "d2691e",
971
+ coral: "ff7f50",
972
+ cornflowerblue: "6495ed",
973
+ cornsilk: "fff8dc",
974
+ crimson: "dc143c",
975
+ cyan: "0ff",
976
+ darkblue: "00008b",
977
+ darkcyan: "008b8b",
978
+ darkgoldenrod: "b8860b",
979
+ darkgray: "a9a9a9",
980
+ darkgreen: "006400",
981
+ darkgrey: "a9a9a9",
982
+ darkkhaki: "bdb76b",
983
+ darkmagenta: "8b008b",
984
+ darkolivegreen: "556b2f",
985
+ darkorange: "ff8c00",
986
+ darkorchid: "9932cc",
987
+ darkred: "8b0000",
988
+ darksalmon: "e9967a",
989
+ darkseagreen: "8fbc8f",
990
+ darkslateblue: "483d8b",
991
+ darkslategray: "2f4f4f",
992
+ darkslategrey: "2f4f4f",
993
+ darkturquoise: "00ced1",
994
+ darkviolet: "9400d3",
995
+ deeppink: "ff1493",
996
+ deepskyblue: "00bfff",
997
+ dimgray: "696969",
998
+ dimgrey: "696969",
999
+ dodgerblue: "1e90ff",
1000
+ firebrick: "b22222",
1001
+ floralwhite: "fffaf0",
1002
+ forestgreen: "228b22",
1003
+ fuchsia: "f0f",
1004
+ gainsboro: "dcdcdc",
1005
+ ghostwhite: "f8f8ff",
1006
+ gold: "ffd700",
1007
+ goldenrod: "daa520",
1008
+ gray: "808080",
1009
+ green: "008000",
1010
+ greenyellow: "adff2f",
1011
+ grey: "808080",
1012
+ honeydew: "f0fff0",
1013
+ hotpink: "ff69b4",
1014
+ indianred: "cd5c5c",
1015
+ indigo: "4b0082",
1016
+ ivory: "fffff0",
1017
+ khaki: "f0e68c",
1018
+ lavender: "e6e6fa",
1019
+ lavenderblush: "fff0f5",
1020
+ lawngreen: "7cfc00",
1021
+ lemonchiffon: "fffacd",
1022
+ lightblue: "add8e6",
1023
+ lightcoral: "f08080",
1024
+ lightcyan: "e0ffff",
1025
+ lightgoldenrodyellow: "fafad2",
1026
+ lightgray: "d3d3d3",
1027
+ lightgreen: "90ee90",
1028
+ lightgrey: "d3d3d3",
1029
+ lightpink: "ffb6c1",
1030
+ lightsalmon: "ffa07a",
1031
+ lightseagreen: "20b2aa",
1032
+ lightskyblue: "87cefa",
1033
+ lightslategray: "789",
1034
+ lightslategrey: "789",
1035
+ lightsteelblue: "b0c4de",
1036
+ lightyellow: "ffffe0",
1037
+ lime: "0f0",
1038
+ limegreen: "32cd32",
1039
+ linen: "faf0e6",
1040
+ magenta: "f0f",
1041
+ maroon: "800000",
1042
+ mediumaquamarine: "66cdaa",
1043
+ mediumblue: "0000cd",
1044
+ mediumorchid: "ba55d3",
1045
+ mediumpurple: "9370db",
1046
+ mediumseagreen: "3cb371",
1047
+ mediumslateblue: "7b68ee",
1048
+ mediumspringgreen: "00fa9a",
1049
+ mediumturquoise: "48d1cc",
1050
+ mediumvioletred: "c71585",
1051
+ midnightblue: "191970",
1052
+ mintcream: "f5fffa",
1053
+ mistyrose: "ffe4e1",
1054
+ moccasin: "ffe4b5",
1055
+ navajowhite: "ffdead",
1056
+ navy: "000080",
1057
+ oldlace: "fdf5e6",
1058
+ olive: "808000",
1059
+ olivedrab: "6b8e23",
1060
+ orange: "ffa500",
1061
+ orangered: "ff4500",
1062
+ orchid: "da70d6",
1063
+ palegoldenrod: "eee8aa",
1064
+ palegreen: "98fb98",
1065
+ paleturquoise: "afeeee",
1066
+ palevioletred: "db7093",
1067
+ papayawhip: "ffefd5",
1068
+ peachpuff: "ffdab9",
1069
+ peru: "cd853f",
1070
+ pink: "ffc0cb",
1071
+ plum: "dda0dd",
1072
+ powderblue: "b0e0e6",
1073
+ purple: "800080",
1074
+ rebeccapurple: "663399",
1075
+ red: "f00",
1076
+ rosybrown: "bc8f8f",
1077
+ royalblue: "4169e1",
1078
+ saddlebrown: "8b4513",
1079
+ salmon: "fa8072",
1080
+ sandybrown: "f4a460",
1081
+ seagreen: "2e8b57",
1082
+ seashell: "fff5ee",
1083
+ sienna: "a0522d",
1084
+ silver: "c0c0c0",
1085
+ skyblue: "87ceeb",
1086
+ slateblue: "6a5acd",
1087
+ slategray: "708090",
1088
+ slategrey: "708090",
1089
+ snow: "fffafa",
1090
+ springgreen: "00ff7f",
1091
+ steelblue: "4682b4",
1092
+ tan: "d2b48c",
1093
+ teal: "008080",
1094
+ thistle: "d8bfd8",
1095
+ tomato: "ff6347",
1096
+ turquoise: "40e0d0",
1097
+ violet: "ee82ee",
1098
+ wheat: "f5deb3",
1099
+ white: "fff",
1100
+ whitesmoke: "f5f5f5",
1101
+ yellow: "ff0",
1102
+ yellowgreen: "9acd32"
1103
+ };
1104
+
1105
+ // Make it easy to access colors via `hexNames[hex]`
1106
+ var hexNames = tinycolor.hexNames = flip(names);
1107
+
1108
+ // Utilities
1109
+ // ---------
1110
+
1111
+ // `{ 'name1': 'val1' }` becomes `{ 'val1': 'name1' }`
1112
+ function flip(o) {
1113
+ var flipped = {};
1114
+ for (var i in o) {
1115
+ if (o.hasOwnProperty(i)) {
1116
+ flipped[o[i]] = i;
1117
+ }
1118
+ }
1119
+ return flipped;
1120
+ }
1121
+
1122
+ // Return a valid alpha value [0,1] with all invalid values being set to 1
1123
+ function boundAlpha(a) {
1124
+ a = parseFloat(a);
1125
+ if (isNaN(a) || a < 0 || a > 1) {
1126
+ a = 1;
1127
+ }
1128
+ return a;
1129
+ }
1130
+
1131
+ // Take input from [0, n] and return it as [0, 1]
1132
+ function bound01(n, max) {
1133
+ if (isOnePointZero(n)) n = "100%";
1134
+ var processPercent = isPercentage(n);
1135
+ n = Math.min(max, Math.max(0, parseFloat(n)));
1136
+
1137
+ // Automatically convert percentage into number
1138
+ if (processPercent) {
1139
+ n = parseInt(n * max, 10) / 100;
1140
+ }
1141
+
1142
+ // Handle floating point rounding errors
1143
+ if (Math.abs(n - max) < 0.000001) {
1144
+ return 1;
1145
+ }
1146
+
1147
+ // Convert into [0, 1] range if it isn't already
1148
+ return n % max / parseFloat(max);
1149
+ }
1150
+
1151
+ // Force a number between 0 and 1
1152
+ function clamp01(val) {
1153
+ return Math.min(1, Math.max(0, val));
1154
+ }
1155
+
1156
+ // Parse a base-16 hex value into a base-10 integer
1157
+ function parseIntFromHex(val) {
1158
+ return parseInt(val, 16);
1159
+ }
1160
+
1161
+ // Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1
1162
+ // <http://stackoverflow.com/questions/7422072/javascript-how-to-detect-number-as-a-decimal-including-1-0>
1163
+ function isOnePointZero(n) {
1164
+ return typeof n == "string" && n.indexOf(".") != -1 && parseFloat(n) === 1;
1165
+ }
1166
+
1167
+ // Check to see if string passed in is a percentage
1168
+ function isPercentage(n) {
1169
+ return typeof n === "string" && n.indexOf("%") != -1;
1170
+ }
1171
+
1172
+ // Force a hex value to have 2 characters
1173
+ function pad2(c) {
1174
+ return c.length == 1 ? "0" + c : "" + c;
1175
+ }
1176
+
1177
+ // Replace a decimal with it's percentage value
1178
+ function convertToPercentage(n) {
1179
+ if (n <= 1) {
1180
+ n = n * 100 + "%";
1181
+ }
1182
+ return n;
1183
+ }
1184
+
1185
+ // Converts a decimal to a hex value
1186
+ function convertDecimalToHex(d) {
1187
+ return Math.round(parseFloat(d) * 255).toString(16);
1188
+ }
1189
+ // Converts a hex value to a decimal
1190
+ function convertHexToDecimal(h) {
1191
+ return parseIntFromHex(h) / 255;
1192
+ }
1193
+ var matchers = function () {
1194
+ // <http://www.w3.org/TR/css3-values/#integers>
1195
+ var CSS_INTEGER = "[-\\+]?\\d+%?";
1196
+
1197
+ // <http://www.w3.org/TR/css3-values/#number-value>
1198
+ var CSS_NUMBER = "[-\\+]?\\d*\\.\\d+%?";
1199
+
1200
+ // Allow positive/negative integer/number. Don't capture the either/or, just the entire outcome.
1201
+ var CSS_UNIT = "(?:" + CSS_NUMBER + ")|(?:" + CSS_INTEGER + ")";
1202
+
1203
+ // Actual matching.
1204
+ // Parentheses and commas are optional, but not required.
1205
+ // Whitespace can take the place of commas or opening paren
1206
+ var PERMISSIVE_MATCH3 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?";
1207
+ var PERMISSIVE_MATCH4 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?";
1208
+ return {
1209
+ CSS_UNIT: new RegExp(CSS_UNIT),
1210
+ rgb: new RegExp("rgb" + PERMISSIVE_MATCH3),
1211
+ rgba: new RegExp("rgba" + PERMISSIVE_MATCH4),
1212
+ hsl: new RegExp("hsl" + PERMISSIVE_MATCH3),
1213
+ hsla: new RegExp("hsla" + PERMISSIVE_MATCH4),
1214
+ hsv: new RegExp("hsv" + PERMISSIVE_MATCH3),
1215
+ hsva: new RegExp("hsva" + PERMISSIVE_MATCH4),
1216
+ hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
1217
+ hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,
1218
+ hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
1219
+ hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/
1220
+ };
1221
+ }();
1222
+
1223
+ // `isValidCSSUnit`
1224
+ // Take in a single string / number and check to see if it looks like a CSS unit
1225
+ // (see `matchers` above for definition).
1226
+ function isValidCSSUnit(color) {
1227
+ return !!matchers.CSS_UNIT.exec(color);
1228
+ }
1229
+
1230
+ // `stringInputToObject`
1231
+ // Permissive string parsing. Take in a number of formats, and output an object
1232
+ // based on detected format. Returns `{ r, g, b }` or `{ h, s, l }` or `{ h, s, v}`
1233
+ function stringInputToObject(color) {
1234
+ color = color.replace(trimLeft, "").replace(trimRight, "").toLowerCase();
1235
+ var named = false;
1236
+ if (names[color]) {
1237
+ color = names[color];
1238
+ named = true;
1239
+ } else if (color == "transparent") {
1240
+ return {
1241
+ r: 0,
1242
+ g: 0,
1243
+ b: 0,
1244
+ a: 0,
1245
+ format: "name"
1246
+ };
1247
+ }
1248
+
1249
+ // Try to match string input using regular expressions.
1250
+ // Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360]
1251
+ // Just return an object and let the conversion functions handle that.
1252
+ // This way the result will be the same whether the tinycolor is initialized with string or object.
1253
+ var match;
1254
+ if (match = matchers.rgb.exec(color)) {
1255
+ return {
1256
+ r: match[1],
1257
+ g: match[2],
1258
+ b: match[3]
1259
+ };
1260
+ }
1261
+ if (match = matchers.rgba.exec(color)) {
1262
+ return {
1263
+ r: match[1],
1264
+ g: match[2],
1265
+ b: match[3],
1266
+ a: match[4]
1267
+ };
1268
+ }
1269
+ if (match = matchers.hsl.exec(color)) {
1270
+ return {
1271
+ h: match[1],
1272
+ s: match[2],
1273
+ l: match[3]
1274
+ };
1275
+ }
1276
+ if (match = matchers.hsla.exec(color)) {
1277
+ return {
1278
+ h: match[1],
1279
+ s: match[2],
1280
+ l: match[3],
1281
+ a: match[4]
1282
+ };
1283
+ }
1284
+ if (match = matchers.hsv.exec(color)) {
1285
+ return {
1286
+ h: match[1],
1287
+ s: match[2],
1288
+ v: match[3]
1289
+ };
1290
+ }
1291
+ if (match = matchers.hsva.exec(color)) {
1292
+ return {
1293
+ h: match[1],
1294
+ s: match[2],
1295
+ v: match[3],
1296
+ a: match[4]
1297
+ };
1298
+ }
1299
+ if (match = matchers.hex8.exec(color)) {
1300
+ return {
1301
+ r: parseIntFromHex(match[1]),
1302
+ g: parseIntFromHex(match[2]),
1303
+ b: parseIntFromHex(match[3]),
1304
+ a: convertHexToDecimal(match[4]),
1305
+ format: named ? "name" : "hex8"
1306
+ };
1307
+ }
1308
+ if (match = matchers.hex6.exec(color)) {
1309
+ return {
1310
+ r: parseIntFromHex(match[1]),
1311
+ g: parseIntFromHex(match[2]),
1312
+ b: parseIntFromHex(match[3]),
1313
+ format: named ? "name" : "hex"
1314
+ };
1315
+ }
1316
+ if (match = matchers.hex4.exec(color)) {
1317
+ return {
1318
+ r: parseIntFromHex(match[1] + "" + match[1]),
1319
+ g: parseIntFromHex(match[2] + "" + match[2]),
1320
+ b: parseIntFromHex(match[3] + "" + match[3]),
1321
+ a: convertHexToDecimal(match[4] + "" + match[4]),
1322
+ format: named ? "name" : "hex8"
1323
+ };
1324
+ }
1325
+ if (match = matchers.hex3.exec(color)) {
1326
+ return {
1327
+ r: parseIntFromHex(match[1] + "" + match[1]),
1328
+ g: parseIntFromHex(match[2] + "" + match[2]),
1329
+ b: parseIntFromHex(match[3] + "" + match[3]),
1330
+ format: named ? "name" : "hex"
1331
+ };
1332
+ }
1333
+ return false;
1334
+ }
1335
+ function validateWCAG2Parms(parms) {
1336
+ // return valid WCAG2 parms for isReadable.
1337
+ // If input parms are invalid, return {"level":"AA", "size":"small"}
1338
+ var level, size;
1339
+ parms = parms || {
1340
+ level: "AA",
1341
+ size: "small"
1342
+ };
1343
+ level = (parms.level || "AA").toUpperCase();
1344
+ size = (parms.size || "small").toLowerCase();
1345
+ if (level !== "AA" && level !== "AAA") {
1346
+ level = "AA";
1347
+ }
1348
+ if (size !== "small" && size !== "large") {
1349
+ size = "small";
1350
+ }
1351
+ return {
1352
+ level: level,
1353
+ size: size
1354
+ };
1355
+ }
1356
+ const ColorListWrap = styled(Flex)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n gap: 0.25em;\n"])));
1357
+ const ColorBoxContainer = styled.div(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n width: 2.1em;\n height: 2.1em;\n cursor: pointer;\n position: relative;\n overflow: hidden;\n border-radius: 3px;\n background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAADFJREFUOE9jZGBgEGHAD97gk2YcNYBhmIQBgWSAP52AwoAQwJvQRg1gACckQoC2gQgAIF8IscwEtKYAAAAASUVORK5CYII=') left center #fff;\n"])));
1358
+ const ColorBox$1 = styled.div(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n content: '';\n position: absolute;\n inset: 0;\n z-index: 1;\n"])));
1359
+ const validateColors = colors => colors.reduce((cls, c) => {
1360
+ const color = c.hex ? tinycolor(c.hex) : tinycolor(c);
1361
+ if (color.isValid()) {
1362
+ cls.push({
1363
+ color: c,
1364
+ backgroundColor: color.toRgbString()
1365
+ });
1366
+ }
1367
+ return cls;
1368
+ }, []);
1369
+ const ColorList = _ref2 => {
1370
+ let {
1371
+ colors,
1372
+ onChange
1373
+ } = _ref2;
1374
+ if (!colors) return null;
1375
+ return /* @__PURE__ */jsx(ColorListWrap, {
1376
+ wrap: "wrap",
1377
+ children: validateColors(colors).map((_ref3, idx) => {
1378
+ let {
1379
+ color,
1380
+ backgroundColor
1381
+ } = _ref3;
1382
+ return /* @__PURE__ */jsx(ColorBoxContainer, {
1383
+ onClick: () => {
1384
+ onChange(color);
1385
+ },
1386
+ children: /* @__PURE__ */jsx(ColorBox$1, {
1387
+ style: {
1388
+ background: backgroundColor
1389
+ }
1390
+ })
1391
+ }, "".concat(backgroundColor, "-").concat(idx));
1392
+ })
1393
+ });
1394
+ };
1395
+ const ColorBox = styled(Box)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n"])));
1396
+ const ReadOnlyContainer = styled(Flex)(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n margin-top: 6rem;\n background-color: var(--card-bg-color);\n position: relative;\n width: 100%;\n"])));
1397
+ const ColorPickerInner = props => {
1398
+ var _a, _b, _c;
1399
+ const {
1400
+ width,
1401
+ color: {
1402
+ rgb,
1403
+ hex,
1404
+ hsv,
1405
+ hsl
1406
+ },
1407
+ onChange,
1408
+ onUnset,
1409
+ disableAlpha,
1410
+ colorList,
1411
+ readOnly
1412
+ } = props;
1413
+ if (!hsl || !hsv) {
1414
+ return null;
1415
+ }
1416
+ return /* @__PURE__ */jsx("div", {
1417
+ style: {
1418
+ width
1419
+ },
1420
+ children: /* @__PURE__ */jsx(Card, {
1421
+ padding: 1,
1422
+ border: true,
1423
+ radius: 1,
1424
+ children: /* @__PURE__ */jsxs(Stack, {
1425
+ space: 2,
1426
+ children: [!readOnly && /* @__PURE__ */jsxs(Fragment, {
1427
+ children: [/* @__PURE__ */jsx(Card, {
1428
+ overflow: "hidden",
1429
+ style: {
1430
+ position: "relative",
1431
+ height: "5em"
1432
+ },
1433
+ children: /* @__PURE__ */jsx(Saturation, {
1434
+ onChange,
1435
+ hsl,
1436
+ hsv
1437
+ })
1438
+ }), /* @__PURE__ */jsx(Card, {
1439
+ shadow: 1,
1440
+ radius: 3,
1441
+ overflow: "hidden",
1442
+ style: {
1443
+ position: "relative",
1444
+ height: "10px"
1445
+ },
1446
+ children: /* @__PURE__ */jsx(Hue, {
1447
+ hsl,
1448
+ onChange: !readOnly && onChange
1449
+ })
1450
+ }), !disableAlpha && /* @__PURE__ */jsx(Card, {
1451
+ shadow: 1,
1452
+ radius: 3,
1453
+ overflow: "hidden",
1454
+ style: {
1455
+ position: "relative",
1456
+ height: "10px",
1457
+ background: "#fff"
1458
+ },
1459
+ children: /* @__PURE__ */jsx(Alpha, {
1460
+ rgb,
1461
+ hsl,
1462
+ onChange
1463
+ })
1464
+ })]
1465
+ }), /* @__PURE__ */jsxs(Flex, {
1466
+ children: [/* @__PURE__ */jsxs(Card, {
1467
+ flex: 1,
1468
+ radius: 2,
1469
+ overflow: "hidden",
1470
+ style: {
1471
+ position: "relative",
1472
+ minWidth: "4em",
1473
+ background: "#fff"
1474
+ },
1475
+ children: [/* @__PURE__ */jsx(Checkboard, {}), /* @__PURE__ */jsx(ColorBox, {
1476
+ style: {
1477
+ backgroundColor: "rgba(".concat(rgb == null ? void 0 : rgb.r, ",").concat(rgb == null ? void 0 : rgb.g, ",").concat(rgb == null ? void 0 : rgb.b, ",").concat(rgb == null ? void 0 : rgb.a, ")")
1478
+ }
1479
+ }), readOnly && /* @__PURE__ */jsx(ReadOnlyContainer, {
1480
+ padding: 2,
1481
+ paddingBottom: 1,
1482
+ sizing: "border",
1483
+ justify: "space-between",
1484
+ children: /* @__PURE__ */jsxs(Stack, {
1485
+ space: 3,
1486
+ marginTop: 1,
1487
+ children: [/* @__PURE__ */jsx(Text, {
1488
+ size: 3,
1489
+ weight: "bold",
1490
+ children: hex
1491
+ }), /* @__PURE__ */jsxs(Inline, {
1492
+ space: 3,
1493
+ children: [/* @__PURE__ */jsxs(Text, {
1494
+ size: 1,
1495
+ children: [/* @__PURE__ */jsx("strong", {
1496
+ children: "RGB: "
1497
+ }), rgb == null ? void 0 : rgb.r, " ", rgb == null ? void 0 : rgb.g, " ", rgb == null ? void 0 : rgb.b]
1498
+ }), /* @__PURE__ */jsxs(Text, {
1499
+ size: 1,
1500
+ children: [/* @__PURE__ */jsx("strong", {
1501
+ children: "HSL: "
1502
+ }), " ", Math.round((_a = hsl == null ? void 0 : hsl.h) != null ? _a : 0), " ", Math.round((_b = hsl == null ? void 0 : hsl.s) != null ? _b : 0), "%", " ", Math.round((_c = hsl == null ? void 0 : hsl.l) != null ? _c : 0)]
1503
+ })]
1504
+ })]
1505
+ })
1506
+ })]
1507
+ }), !readOnly && /* @__PURE__ */jsxs(Flex, {
1508
+ align: "flex-start",
1509
+ marginLeft: 2,
1510
+ children: [/* @__PURE__ */jsx(Box, {
1511
+ style: {
1512
+ width: 200
1513
+ },
1514
+ children: /* @__PURE__ */jsx(ColorPickerFields, {
1515
+ rgb,
1516
+ hsl,
1517
+ hex,
1518
+ onChange,
1519
+ disableAlpha
1520
+ })
1521
+ }), /* @__PURE__ */jsx(Box, {
1522
+ marginLeft: 2,
1523
+ children: /* @__PURE__ */jsx(Button, {
1524
+ onClick: onUnset,
1525
+ title: "Delete color",
1526
+ icon: TrashIcon,
1527
+ tone: "critical"
1528
+ })
1529
+ })]
1530
+ })]
1531
+ }), colorList && /* @__PURE__ */jsx(ColorList, {
1532
+ colors: colorList,
1533
+ onChange
1534
+ })]
1535
+ })
1536
+ })
1537
+ });
1538
+ };
1539
+ const ColorPicker = CustomPicker(ColorPickerInner);
1540
+ const DEFAULT_COLOR = {
1541
+ hex: "#24a3e3",
1542
+ hsl: {
1543
+ h: 200,
1544
+ s: 0.7732,
1545
+ l: 0.5156,
1546
+ a: 1
1547
+ },
1548
+ hsv: {
1549
+ h: 200,
1550
+ s: 0.8414,
1551
+ v: 0.8901,
1552
+ a: 1
1553
+ },
1554
+ rgb: {
1555
+ r: 46,
1556
+ g: 163,
1557
+ b: 227,
1558
+ a: 1
1559
+ },
1560
+ source: "hex"
1561
+ };
1562
+ function ColorInput(props) {
1563
+ var _a, _b;
1564
+ const {
1565
+ onChange,
1566
+ readOnly
1567
+ } = props;
1568
+ const value = props.value;
1569
+ const type = props.schemaType;
1570
+ const focusRef = useRef(null);
1571
+ const [color, setColor] = useState(value);
1572
+ useEffect(() => setColor(value), [value]);
1573
+ const emitSetColor = useCallback(nextColor => {
1574
+ var _a2;
1575
+ const fieldPatches = type.fields.filter(field => field.name in nextColor).map(field => {
1576
+ const nextFieldValue = nextColor[field.name];
1577
+ const isObject = field.type.jsonType === "object";
1578
+ return set(isObject ? Object.assign({
1579
+ _type: field.type.name
1580
+ }, nextFieldValue) : nextFieldValue, [field.name]);
1581
+ });
1582
+ onChange([setIfMissing({
1583
+ _type: type.name
1584
+ }), set(type.name, ["_type"]), set((_a2 = nextColor.rgb) == null ? void 0 : _a2.a, ["alpha"]), ...fieldPatches]);
1585
+ }, [onChange, type]);
1586
+ const debouncedColorChange = useMemo(() => debounce(emitSetColor, 100), [emitSetColor]);
1587
+ const handleColorChange = useCallback(nextColor => {
1588
+ setColor(nextColor);
1589
+ debouncedColorChange(nextColor);
1590
+ }, [debouncedColorChange, setColor]);
1591
+ const handleCreateColor = useCallback(() => {
1592
+ setColor(DEFAULT_COLOR);
1593
+ emitSetColor(DEFAULT_COLOR);
1594
+ }, [emitSetColor]);
1595
+ const handleUnset = useCallback(() => {
1596
+ setColor(void 0);
1597
+ onChange(unset());
1598
+ }, [onChange]);
1599
+ return /* @__PURE__ */jsx(Fragment, {
1600
+ children: value && value.hex ? /* @__PURE__ */jsx(ColorPicker, {
1601
+ color,
1602
+ onChange: handleColorChange,
1603
+ readOnly: readOnly || typeof type.readOnly === "boolean" && type.readOnly,
1604
+ disableAlpha: !!((_a = type.options) == null ? void 0 : _a.disableAlpha),
1605
+ colorList: (_b = type.options) == null ? void 0 : _b.colorList,
1606
+ onUnset: handleUnset
1607
+ }) : /* @__PURE__ */jsx(Button, {
1608
+ icon: AddIcon,
1609
+ mode: "ghost",
1610
+ text: "Create color",
1611
+ ref: focusRef,
1612
+ disabled: Boolean(readOnly),
1613
+ onClick: handleCreateColor
1614
+ })
1615
+ });
1616
+ }
1617
+ const round = function () {
1618
+ let val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
1619
+ return Math.round(val * 100);
1620
+ };
1621
+ const colorTypeName = "color";
1622
+ const color = defineType({
1623
+ name: colorTypeName,
1624
+ type: "object",
1625
+ title: "Color",
1626
+ components: {
1627
+ input: ColorInput
1628
+ },
1629
+ fields: [{
1630
+ title: "Hex",
1631
+ name: "hex",
1632
+ type: "string"
1633
+ }, {
1634
+ title: "Alpha",
1635
+ name: "alpha",
1636
+ type: "number"
1637
+ }, {
1638
+ title: "Hue Saturation Lightness",
1639
+ name: "hsl",
1640
+ type: "hslaColor"
1641
+ }, {
1642
+ title: "Hue Saturation Value",
1643
+ name: "hsv",
1644
+ type: "hsvaColor"
1645
+ }, {
1646
+ title: "Red Green Blue (rgb)",
1647
+ name: "rgb",
1648
+ type: "rgbaColor"
1649
+ }],
1650
+ preview: {
1651
+ select: {
1652
+ title: "hex",
1653
+ alpha: "alpha",
1654
+ hex: "hex",
1655
+ hsl: "hsl"
1656
+ },
1657
+ prepare(_ref4) {
1658
+ let {
1659
+ title,
1660
+ hex,
1661
+ hsl,
1662
+ alpha
1663
+ } = _ref4;
1664
+ let subtitle = hex || "No color set";
1665
+ if (hsl) {
1666
+ subtitle = "H:".concat(round(hsl.h), " S:").concat(round(hsl.s), " L:").concat(round(hsl.l), " A:").concat(round(alpha));
1667
+ }
1668
+ return {
1669
+ title,
1670
+ subtitle,
1671
+ media: () => /* @__PURE__ */jsx("div", {
1672
+ style: {
1673
+ backgroundColor: hex != null ? hex : "#000",
1674
+ opacity: alpha != null ? alpha : 1,
1675
+ position: "absolute",
1676
+ height: "100%",
1677
+ width: "100%",
1678
+ top: "0",
1679
+ left: "0"
1680
+ }
1681
+ })
1682
+ };
1683
+ }
1684
+ }
1685
+ });
1686
+ const hsvaColor = defineType({
1687
+ title: "Hue Saturation Value",
1688
+ name: "hsvaColor",
1689
+ type: "object",
1690
+ fields: [{
1691
+ name: "h",
1692
+ type: "number",
1693
+ title: "Hue"
1694
+ }, {
1695
+ name: "s",
1696
+ type: "number",
1697
+ title: "Saturation"
1698
+ }, {
1699
+ name: "v",
1700
+ type: "number",
1701
+ title: "Value"
1702
+ }, {
1703
+ name: "a",
1704
+ type: "number",
1705
+ title: "Alpha"
1706
+ }]
1707
+ });
1708
+ const colorInput = definePlugin({
1709
+ name: "@sanity/color-input",
1710
+ schema: {
1711
+ types: [hslaColor, hsvaColor, rgbaColor, color]
1712
+ }
1713
+ });
1714
+ export { ColorInput, color, colorInput, hslaColor, hsvaColor, rgbaColor };
2
1715
  //# sourceMappingURL=index.esm.js.map