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