@thednp/color-picker 0.0.1-alpha2 → 0.0.2-alpha1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +32 -15
- package/dist/css/color-picker.css +38 -15
- package/dist/css/color-picker.min.css +2 -2
- package/dist/css/color-picker.rtl.css +38 -15
- package/dist/css/color-picker.rtl.min.css +2 -2
- package/dist/js/color-esm.js +1178 -0
- package/dist/js/color-esm.min.js +2 -0
- package/dist/js/color-palette-esm.js +1252 -0
- package/dist/js/color-palette-esm.min.js +2 -0
- package/dist/js/color-palette.js +1260 -0
- package/dist/js/color-palette.min.js +2 -0
- package/dist/js/color-picker-element-esm.js +433 -424
- package/dist/js/color-picker-element-esm.min.js +2 -2
- package/dist/js/color-picker-element.js +435 -426
- package/dist/js/color-picker-element.min.js +2 -2
- package/dist/js/color-picker-esm.js +745 -739
- package/dist/js/color-picker-esm.min.js +2 -2
- package/dist/js/color-picker.js +747 -741
- package/dist/js/color-picker.min.js +2 -2
- package/dist/js/color.js +1186 -0
- package/dist/js/color.min.js +2 -0
- package/package.json +19 -3
- package/src/js/color-palette.js +28 -12
- package/src/js/color-picker-element.js +8 -4
- package/src/js/color-picker.js +84 -172
- package/src/js/color.js +125 -131
- package/src/js/util/getColorControls.js +3 -3
- package/src/js/util/getColorForm.js +0 -1
- package/src/js/util/getColorMenu.js +31 -33
- package/src/js/util/roundPart.js +9 -0
- package/src/js/util/setCSSProperties.js +12 -0
- package/src/js/util/setMarkup.js +122 -0
- package/src/js/util/tabindex.js +3 -0
- package/src/js/util/version.js +6 -0
- package/src/scss/color-picker.scss +35 -16
- package/types/cp.d.ts +48 -20
- package/src/js/util/templates.js +0 -10
@@ -0,0 +1,2 @@
|
|
1
|
+
// Color v0.0.2alpha1 | thednp © 2022 | MIT-License
|
2
|
+
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t="undefined"!=typeof globalThis?globalThis:t||self).Color=r()}(this,(function(){"use strict";const{head:t}=document;function r(t,r){const e=getComputedStyle(t);return r in e?e[r]:""}const e=(t,r)=>Object.assign(t,r),n=(t,r)=>{e(t.style,r)},o=["transparent","currentColor","inherit","revert","initial"];function s(t){const r=Math.floor(t);return t-r<.5?r:Math.round(t)}const a=["rgb","hex","hsl","hsv","hwb"],i="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",h="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",u=`(?:${h})|(?:${i})`,c="(?:[,|\\s]+)",b=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${u})${c}(${h})${c}(${h})(?:[,|\\/\\s]*)?(${h})?(?:[\\s|\\)\\s]+)?`,g={CSS_UNIT:new RegExp(u),hwb:new RegExp("hwb"+b),rgb:new RegExp("rgb(?:a)?"+b),hsl:new RegExp("hsl(?:a)?"+b),hsv:new RegExp("hsv(?:a)?"+b),hex3:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex6:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,hex4:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex8:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/};function l(t){return(""+t).includes(".")&&1===parseFloat(t)}function f(t){return(""+t).includes("%")}function m(e){return!o.includes(e)&&!["#",...a].some(t=>e.includes(t))&&["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(o=>{n(t,{color:e});const s=r(t,"color");return n(t,{color:""}),s!==o})}function p(t){return Boolean(g.CSS_UNIT.exec(String(t)))}function d(t,r){let e=t;l(t)&&(e="100%");const n=f(e);return e=360===r?parseFloat(e):Math.min(r,Math.max(0,parseFloat(e))),n&&(e=e*r/100),Math.abs(e-r)<1e-6?1:(e=360===r?(e<0?e%r+r:e%r)/r:e%r/r,e)}function x(t){let r=parseFloat(""+t);return(Number.isNaN(r)||r<0||r>1)&&(r=1),r}function S(t){return Math.min(1,Math.max(0,t))}function $(e){n(t,{color:e});const o=r(t,"color");return n(t,{color:""}),o}function A(t){return s(255*t).toString(16)}function w(t){return H(t)/255}function H(t){return parseInt(t,16)}function y(t){return 1===t.length?"0"+t:String(t)}function v(t,r,e){const n=t/255,o=r/255,s=e/255,a=Math.max(n,o,s),i=Math.min(n,o,s);let h=0,u=0;const c=(a+i)/2;if(a===i)u=0,h=0;else{const t=a-i;switch(u=c>.5?t/(2-a-i):t/(a+i),a){case n:h=(o-s)/t+(o<s?6:0);break;case o:h=(s-n)/t+2;break;case s:h=(n-o)/t+4}h/=6}return{h:h,s:u,l:c}}function R(t,r,e){let n=e;return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*n*(r-t):n<.5?r:n<2/3?t+(r-t)*(2/3-n)*6:t}function M(t,r,e){let n=0,o=0,s=0;if(0===r)o=e,s=e,n=e;else{const a=e<.5?e*(1+r):e+r-e*r,i=2*e-a;n=R(i,a,t+1/3),o=R(i,a,t),s=R(i,a,t-1/3)}return[n,o,s]=[n,o,s].map(t=>255*t),{r:n,g:o,b:s}}function F(t,r,e){const n=t/255,o=r/255,s=e/255;let a=0,i=0;const h=Math.min(n,o,s),u=Math.max(n,o,s),c=1-u;if(u===h)return{h:0,w:h,b:c};n===h?(a=o-s,i=3):(a=o===h?s-n:n-o,i=o===h?5:1);const b=(i-a/(u-h))/6;return{h:1===b?0:b,w:h,b:c}}function T(t,r,e){if(r+e>=1){const t=r/(r+e)*255;return{r:t,g:t,b:t}}let{r:n,g:o,b:s}=M(t,1,.5);return[n,o,s]=[n,o,s].map(t=>t/255*(1-r-e)+r).map(t=>255*t),{r:n,g:o,b:s}}function C(t,r,e){const n=t/255,o=r/255,s=e/255,a=Math.max(n,o,s),i=Math.min(n,o,s);let h=0;const u=a,c=a-i,b=0===a?0:c/a;if(a===i)h=0;else{switch(a){case n:h=(o-s)/c+(o<s?6:0);break;case o:h=(s-n)/c+2;break;case s:h=(n-o)/c+4}h/=6}return{h:h,s:b,v:u}}function E(t,r,e){const n=6*t,o=r,s=e,a=Math.floor(n),i=n-a,h=s*(1-o),u=s*(1-i*o),c=s*(1-(1-i)*o),b=a%6;let g=[s,u,h,h,c,s][b],l=[c,s,s,u,h,h][b],f=[h,h,c,s,s,u][b];return[g,l,f]=[g,l,f].map(t=>255*t),{r:g,g:l,b:f}}function N(t,r,e,n){const o=[y(s(t).toString(16)),y(s(r).toString(16)),y(s(e).toString(16))];return n&&o[0].charAt(0)===o[0].charAt(1)&&o[1].charAt(0)===o[1].charAt(1)&&o[2].charAt(0)===o[2].charAt(1)?o[0].charAt(0)+o[1].charAt(0)+o[2].charAt(0):o.join("")}function k(t,r,e,n,o){const a=[y(s(t).toString(16)),y(s(r).toString(16)),y(s(e).toString(16)),y(A(n))];return o&&a[0].charAt(0)===a[0].charAt(1)&&a[1].charAt(0)===a[1].charAt(1)&&a[2].charAt(0)===a[2].charAt(1)&&a[3].charAt(0)===a[3].charAt(1)?a[0].charAt(0)+a[1].charAt(0)+a[2].charAt(0)+a[3].charAt(0):a.join("")}function I(t){let r=t.trim().toLowerCase();if(0===r.length)return{r:0,g:0,b:0,a:1};let e=!1;if(m(r))r=$(r),e=!0;else if(o.includes(r)){return{r:0,g:0,b:0,a:"transparent"===r?0:1,format:"rgb",ok:!0}}let[,n,s,a,i]=g.rgb.exec(r)||[];return n&&s&&a?{r:n,g:s,b:a,a:void 0!==i?i:1,format:"rgb"}:([,n,s,a,i]=g.hsl.exec(r)||[],n&&s&&a?{h:n,s:s,l:a,a:void 0!==i?i:1,format:"hsl"}:([,n,s,a,i]=g.hsv.exec(r)||[],n&&s&&a?{h:n,s:s,v:a,a:void 0!==i?i:1,format:"hsv"}:([,n,s,a,i]=g.hwb.exec(r)||[],n&&s&&a?{h:n,w:s,b:a,a:void 0!==i?i:1,format:"hwb"}:([,n,s,a,i]=g.hex8.exec(r)||[],n&&s&&a&&i?{r:H(n),g:H(s),b:H(a),a:w(i),format:e?"rgb":"hex"}:([,n,s,a]=g.hex6.exec(r)||[],n&&s&&a?{r:H(n),g:H(s),b:H(a),format:e?"rgb":"hex"}:([,n,s,a,i]=g.hex4.exec(r)||[],n&&s&&a&&i?{r:H(n+n),g:H(s+s),b:H(a+a),a:w(i+i),format:e?"rgb":"hex"}:([,n,s,a]=g.hex3.exec(r)||[],!!(n&&s&&a)&&{r:H(n+n),g:H(s+s),b:H(a+a),format:e?"rgb":"hex"})))))))}function j(t){let r={r:0,g:0,b:0},e=t,n=1,o=null,s=null,i=null,h=null,u=null,c=null,b=null,g=null,l=!1;const m="object"==typeof e&&e.format;let S=m&&a.includes(m)?m:"rgb";return"string"==typeof t&&(e=I(t),e&&(l=!0)),"object"==typeof e&&(p(e.r)&&p(e.g)&&p(e.b)?(({r:b,g:g,b:u}=e),[b,g,u]=[b,g,u].map(t=>255*d(t,f(t)?100:255)),r={r:b,g:g,b:u},l=!0,S="rgb"):p(e.h)&&p(e.s)&&p(e.v)?(({h:c,s:o,v:s}=e),c="number"==typeof c?c:d(c,360),o="number"==typeof o?o:d(o,100),s="number"==typeof s?s:d(s,100),r=E(c,o,s),l=!0,S="hsv"):p(e.h)&&p(e.s)&&p(e.l)?(({h:c,s:o,l:i}=e),c="number"==typeof c?c:d(c,360),o="number"==typeof o?o:d(o,100),i="number"==typeof i?i:d(i,100),r=M(c,o,i),l=!0,S="hsl"):p(e.h)&&p(e.w)&&p(e.b)&&(({h:c,w:h,b:u}=e),c="number"==typeof c?c:d(c,360),h="number"==typeof h?h:d(h,100),u="number"==typeof u?u:d(u,100),r=T(c,h,u),l=!0,S="hwb"),p(e.a)&&(n=e.a,n=f(""+n)||parseFloat(n)>1?d(n,100):n)),void 0===e&&(l=!0),{ok:l,format:S,r:Math.min(255,Math.max(r.r,0)),g:Math.min(255,Math.max(r.g,0)),b:Math.min(255,Math.max(r.b,0)),a:x(n)}}class _{constructor(t,r){let e=t;const n=r&&a.includes(r)?r:"rgb";if(e instanceof _&&(e=j(e)),"number"==typeof e){e=`#${2===(""+e).length?"0":"00"}${e}`}const{r:o,g:s,b:i,a:h,ok:u,format:c}=j(e);this.originalInput=t,this.r=o,this.g=s,this.b=i,this.a=h,this.ok=u,this.format=n||c}get isValid(){return this.ok}get isDark(){return this.brightness<120}get luminance(){const{r:t,g:r,b:e}=this;let n=0,o=0,s=0;const a=t/255,i=r/255,h=e/255;return n=a<=.03928?a/12.92:((a+.055)/1.055)**2.4,o=i<=.03928?i/12.92:((i+.055)/1.055)**2.4,s=h<=.03928?h/12.92:((h+.055)/1.055)**2.4,.2126*n+.7152*o+.0722*s}get brightness(){const{r:t,g:r,b:e}=this;return(299*t+587*r+114*e)/1e3}toRgb(){const{r:t,g:r,b:e,a:n}=this;return{r:t,g:r,b:e,a:s(100*n)/100}}toRgbString(){const{r:t,g:r,b:e,a:n}=this.toRgb(),[o,a,i]=[t,r,e].map(s);return 1===n?`rgb(${o}, ${a}, ${i})`:`rgba(${o}, ${a}, ${i}, ${n})`}toRgbCSS4String(){const{r:t,g:r,b:e,a:n}=this.toRgb(),[o,a,i]=[t,r,e].map(s);return`rgb(${o} ${a} ${i}${1===n?"":` / ${s(100*n)}%`})`}toHex(t){const{r:r,g:e,b:n,a:o}=this.toRgb();return 1===o?N(r,e,n,t):k(r,e,n,o,t)}toHexString(t){return"#"+this.toHex(t)}toHex8(t){const{r:r,g:e,b:n,a:o}=this.toRgb();return k(r,e,n,o,t)}toHex8String(t){return"#"+this.toHex8(t)}toHsv(){const{r:t,g:r,b:e,a:n}=this.toRgb(),{h:o,s:s,v:a}=C(t,r,e);return{h:o,s:s,v:a,a:n}}toHsl(){const{r:t,g:r,b:e,a:n}=this.toRgb(),{h:o,s:s,l:a}=v(t,r,e);return{h:o,s:s,l:a,a:n}}toHslString(){let{h:t,s:r,l:e,a:n}=this.toHsl();return t=s(360*t),r=s(100*r),e=s(100*e),n=s(100*n)/100,1===n?`hsl(${t}, ${r}%, ${e}%)`:`hsla(${t}, ${r}%, ${e}%, ${n})`}toHslCSS4String(){let{h:t,s:r,l:e,a:n}=this.toHsl();t=s(360*t),r=s(100*r),e=s(100*e),n=s(100*n);return`hsl(${t}deg ${r}% ${e}%${n<100?` / ${s(n)}%`:""})`}toHwb(){const{r:t,g:r,b:e,a:n}=this,{h:o,w:s,b:a}=F(t,r,e);return{h:o,w:s,b:a,a:n}}toHwbString(){let{h:t,w:r,b:e,a:n}=this.toHwb();t=s(360*t),r=s(100*r),e=s(100*e),n=s(100*n);return`hwb(${t}deg ${r}% ${e}%${n<100?` / ${s(n)}%`:""})`}setAlpha(t){return this.a=x(t),this}saturate(t){if("number"!=typeof t)return this;const{h:r,s:n,l:o}=this.toHsl(),{r:s,g:a,b:i}=M(r,S(n+t/100),o);return e(this,{r:s,g:a,b:i}),this}desaturate(t){return"number"==typeof t?this.saturate(-t):this}greyscale(){return this.saturate(-100)}lighten(t){if("number"!=typeof t)return this;const{h:r,s:n,l:o}=this.toHsl(),{r:s,g:a,b:i}=M(r,n,S(o+t/100));return e(this,{r:s,g:a,b:i}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(t){if("number"!=typeof t)return this;const{h:r,s:n,l:o}=this.toHsl(),{r:s,g:a,b:i}=M(S((360*r+t)%360/360),n,o);return e(this,{r:s,g:a,b:i}),this}clone(){return new _(this)}toString(t){const{format:r}=this;return"hex"===r?this.toHexString(t):"hsl"===r?this.toHslString():"hwb"===r?this.toHwbString():this.toRgbString()}}return e(_,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:i,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:h,CSS_UNIT2:u,PERMISSIVE_MATCH:b,matchers:g,isOnePointZero:l,isPercentage:f,isValidCSSUnit:p,isColorName:m,pad2:y,clamp01:S,bound01:d,boundAlpha:x,getRGBFromName:$,convertHexToDecimal:w,convertDecimalToHex:A,rgbToHsl:v,rgbToHex:N,rgbToHsv:C,rgbToHwb:F,rgbaToHex:k,hslToRgb:M,hsvToRgb:E,hueToRgb:R,hwbToRgb:T,parseIntFromHex:H,stringInputToObject:I,inputToRGB:j,roundPart:s,getElementStyle:r,setElementStyle:n,ObjectAssign:e}),_}));
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@thednp/color-picker",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.2alpha1",
|
4
4
|
"description": "Modern Color Picker Web Component",
|
5
5
|
"main": "dist/js/color-picker.min.js",
|
6
6
|
"module": "dist/js/color-picker.esm.js",
|
@@ -18,7 +18,9 @@
|
|
18
18
|
"registry": "https://registry.npmjs.org/"
|
19
19
|
},
|
20
20
|
"scripts": {
|
21
|
-
"
|
21
|
+
"instrument": "npx nyc instrument --all --compact=false src/js cypress/instrumented",
|
22
|
+
"test": "npm run instrument && npx cypress run",
|
23
|
+
"coverage:report": "npx nyc report --exclude=src/js/util --reporter=lcov --reporter=text --report-dir=cypress/coverage",
|
22
24
|
"fix:js": "eslint src/ --config .eslintrc --fix",
|
23
25
|
"lint:js": "eslint src/ --config .eslintrc",
|
24
26
|
"fix:css": "stylelint --config .stylelintrc.json -s --fix scss \"src/scss/*.scss\"",
|
@@ -31,6 +33,14 @@
|
|
31
33
|
"build-ce-min": "rollup --environment FORMAT:umd,MIN:true,NAME:ColorPickerElement,INPUTFILE:src/js/color-picker-element.js,OUTPUTFILE:dist/js/color-picker-element.min.js -c",
|
32
34
|
"build-ce-esm": "rollup --environment FORMAT:esm,MIN:false,NAME:ColorPickerElement,INPUTFILE:src/js/color-picker-element.js,OUTPUTFILE:dist/js/color-picker-element-esm.js -c",
|
33
35
|
"build-ce-esm-min": "rollup --environment FORMAT:esm,MIN:true,NAME:ColorPickerElement,INPUTFILE:src/js/color-picker-element.js,OUTPUTFILE:dist/js/color-picker-element-esm.min.js -c",
|
36
|
+
"build-cl": "rollup --environment FORMAT:umd,MIN:false,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color.js -c",
|
37
|
+
"build-cl-min": "rollup --environment FORMAT:umd,MIN:true,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color.min.js -c",
|
38
|
+
"build-cl-esm": "rollup --environment FORMAT:esm,MIN:false,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color-esm.js -c",
|
39
|
+
"build-cl-esm-min": "rollup --environment FORMAT:esm,MIN:true,NAME:Color,INPUTFILE:src/js/color.js,OUTPUTFILE:dist/js/color-esm.min.js -c",
|
40
|
+
"build-cpl": "rollup --environment FORMAT:umd,MIN:false,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette.js -c",
|
41
|
+
"build-cpl-min": "rollup --environment FORMAT:umd,MIN:true,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette.min.js -c",
|
42
|
+
"build-cpl-esm": "rollup --environment FORMAT:esm,MIN:false,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette-esm.js -c",
|
43
|
+
"build-cpl-esm-min": "rollup --environment FORMAT:esm,MIN:true,NAME:ColorPalette,INPUTFILE:src/js/color-palette.js,OUTPUTFILE:dist/js/color-palette-esm.min.js -c",
|
34
44
|
"build-minjs": "rollup --environment FORMAT:umd,MIN:true -c",
|
35
45
|
"build-esm": "rollup --environment FORMAT:esm,MIN:false -c",
|
36
46
|
"build-esmjs": "rollup --environment FORMAT:esm,MIN:true -c",
|
@@ -68,15 +78,21 @@
|
|
68
78
|
"shorter-js": "^0.3.4"
|
69
79
|
},
|
70
80
|
"devDependencies": {
|
81
|
+
"@bahmutov/cypress-esbuild-preprocessor": "^2.1.3",
|
82
|
+
"@cypress/code-coverage": "^3.9.12",
|
71
83
|
"@rollup/plugin-buble": "^0.21.3",
|
84
|
+
"@rollup/plugin-commonjs": "^21.0.3",
|
72
85
|
"@rollup/plugin-json": "^4.1.0",
|
73
86
|
"@rollup/plugin-node-resolve": "^7.1.3",
|
87
|
+
"babel-plugin-istanbul": "^6.1.1",
|
88
|
+
"cypress": "^9.5.3",
|
89
|
+
"esbuild": "^0.14.30",
|
74
90
|
"eslint": "^7.22.0",
|
75
91
|
"eslint-config-airbnb-base": "^14.2.1",
|
76
92
|
"eslint-plugin-import": "^2.22.1",
|
77
93
|
"eslint-plugin-vue": "^7.7.0",
|
78
94
|
"npm-run-all": "^4.1.5",
|
79
|
-
"rollup": "^
|
95
|
+
"rollup": "^2.70.1",
|
80
96
|
"rollup-plugin-terser": "^5.3.1",
|
81
97
|
"sass": "^1.39.0",
|
82
98
|
"stylelint": "^13.12.0",
|
package/src/js/color-palette.js
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
import ObjectAssign from 'shorter-js/src/misc/ObjectAssign';
|
2
|
+
|
3
|
+
import roundPart from './util/roundPart';
|
1
4
|
import Color from './color';
|
2
5
|
|
3
6
|
/**
|
@@ -5,15 +8,15 @@ import Color from './color';
|
|
5
8
|
* Returns a color palette with a given set of parameters.
|
6
9
|
* @example
|
7
10
|
* new ColorPalette(0, 12, 10);
|
8
|
-
* // => { hue: 0, hueSteps: 12, lightSteps: 10, colors:
|
11
|
+
* // => { hue: 0, hueSteps: 12, lightSteps: 10, colors: Array<Color> }
|
9
12
|
*/
|
10
13
|
export default class ColorPalette {
|
11
14
|
/**
|
12
15
|
* The `hue` parameter is optional, which would be set to 0.
|
13
16
|
* @param {number[]} args represeinting hue, hueSteps, lightSteps
|
14
17
|
* * `args.hue` the starting Hue [0, 360]
|
15
|
-
* * `args.hueSteps` Hue Steps Count [5,
|
16
|
-
* * `args.lightSteps` Lightness Steps Count [
|
18
|
+
* * `args.hueSteps` Hue Steps Count [5, 24]
|
19
|
+
* * `args.lightSteps` Lightness Steps Count [5, 12]
|
17
20
|
*/
|
18
21
|
constructor(...args) {
|
19
22
|
let hue = 0;
|
@@ -25,32 +28,43 @@ export default class ColorPalette {
|
|
25
28
|
[hue, hueSteps, lightSteps] = args;
|
26
29
|
} else if (args.length === 2) {
|
27
30
|
[hueSteps, lightSteps] = args;
|
31
|
+
if ([hueSteps, lightSteps].some((n) => n < 1)) {
|
32
|
+
throw TypeError('ColorPalette: when 2 arguments used, both must be larger than 0.');
|
33
|
+
}
|
28
34
|
} else {
|
29
|
-
throw TypeError('
|
35
|
+
throw TypeError('ColorPalette requires minimum 2 arguments');
|
30
36
|
}
|
31
37
|
|
32
|
-
/** @type {
|
38
|
+
/** @type {Color[]} */
|
33
39
|
const colors = [];
|
34
40
|
|
35
41
|
const hueStep = 360 / hueSteps;
|
36
|
-
const
|
37
|
-
const
|
42
|
+
const half = roundPart((lightSteps - (lightSteps % 2 ? 1 : 0)) / 2);
|
43
|
+
const estimatedStep = 100 / (lightSteps + (lightSteps % 2 ? 0 : 1)) / 100;
|
44
|
+
|
45
|
+
let lightStep = 0.25;
|
46
|
+
lightStep = [4, 5].includes(lightSteps) ? 0.2 : lightStep;
|
47
|
+
lightStep = [6, 7].includes(lightSteps) ? 0.15 : lightStep;
|
48
|
+
lightStep = [8, 9].includes(lightSteps) ? 0.11 : lightStep;
|
49
|
+
lightStep = [10, 11].includes(lightSteps) ? 0.09 : lightStep;
|
50
|
+
lightStep = [12, 13].includes(lightSteps) ? 0.075 : lightStep;
|
51
|
+
lightStep = lightSteps > 13 ? estimatedStep : lightStep;
|
38
52
|
|
39
53
|
// light tints
|
40
|
-
for (let i =
|
41
|
-
lightnessArray = [...lightnessArray, (0.5 + lightStep * (i
|
54
|
+
for (let i = 1; i < half + 1; i += 1) {
|
55
|
+
lightnessArray = [...lightnessArray, (0.5 + lightStep * (i))];
|
42
56
|
}
|
43
57
|
|
44
58
|
// dark tints
|
45
|
-
for (let i =
|
46
|
-
lightnessArray = [(0.5 - lightStep * (i
|
59
|
+
for (let i = 1; i < lightSteps - half; i += 1) {
|
60
|
+
lightnessArray = [(0.5 - lightStep * (i)), ...lightnessArray];
|
47
61
|
}
|
48
62
|
|
49
63
|
// feed `colors` Array
|
50
64
|
for (let i = 0; i < hueSteps; i += 1) {
|
51
65
|
const currentHue = ((hue + i * hueStep) % 360) / 360;
|
52
66
|
lightnessArray.forEach((l) => {
|
53
|
-
colors.push(new Color({ h: currentHue, s: 1, l })
|
67
|
+
colors.push(new Color({ h: currentHue, s: 1, l }));
|
54
68
|
});
|
55
69
|
}
|
56
70
|
|
@@ -60,3 +74,5 @@ export default class ColorPalette {
|
|
60
74
|
this.colors = colors;
|
61
75
|
}
|
62
76
|
}
|
77
|
+
|
78
|
+
ObjectAssign(ColorPalette, { Color });
|
@@ -5,16 +5,18 @@ import setAttribute from 'shorter-js/src/attr/setAttribute';
|
|
5
5
|
import getAttribute from 'shorter-js/src/attr/getAttribute';
|
6
6
|
|
7
7
|
import Color from './color';
|
8
|
-
import ColorPicker from './color-picker';
|
9
|
-
import
|
8
|
+
import ColorPicker, { getColorPickerInstance } from './color-picker';
|
9
|
+
import ColorPalette from './color-palette';
|
10
|
+
import Version from './util/version';
|
10
11
|
|
11
12
|
let CPID = 0;
|
12
13
|
|
13
14
|
/**
|
14
15
|
* `ColorPickerElement` Web Component.
|
15
16
|
* @example
|
16
|
-
* <
|
17
|
-
*
|
17
|
+
* <label for="UNIQUE_ID">Label</label>
|
18
|
+
* <color-picker data-format="hex" data-value="#075">
|
19
|
+
* <input id="UNIQUE_ID" type="text" class="color-preview btn-appearance">
|
18
20
|
* </color-picker>
|
19
21
|
*/
|
20
22
|
class ColorPickerElement extends HTMLElement {
|
@@ -95,6 +97,8 @@ class ColorPickerElement extends HTMLElement {
|
|
95
97
|
ObjectAssign(ColorPickerElement, {
|
96
98
|
Color,
|
97
99
|
ColorPicker,
|
100
|
+
ColorPalette,
|
101
|
+
getInstance: getColorPickerInstance,
|
98
102
|
Version,
|
99
103
|
});
|
100
104
|
|