@thednp/color-picker 0.0.1-alpha1
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.md +89 -0
- package/dist/css/color-picker.css +338 -0
- package/dist/js/color-picker-element-esm.js +2 -0
- package/dist/js/color-picker-element.js +3051 -0
- package/dist/js/color-picker-element.min.js +2 -0
- package/dist/js/color-picker.esm.js +2998 -0
- package/dist/js/color-picker.esm.min.js +2 -0
- package/dist/js/color-picker.js +3006 -0
- package/dist/js/color-picker.min.js +2 -0
- package/package.json +79 -0
- package/src/js/color-picker-element.js +61 -0
- package/src/js/color-picker.js +1333 -0
- package/src/js/color.js +860 -0
- package/src/js/index.js +12 -0
- package/src/js/util/colorNames.js +156 -0
- package/src/js/util/getColorControl.js +49 -0
- package/src/js/util/getColorForm.js +58 -0
- package/src/js/util/init.js +14 -0
- package/src/js/util/templates.js +9 -0
- package/src/js/util/vHidden.js +2 -0
- package/src/js/version.js +6 -0
- package/types/cp.d.ts +411 -0
- package/types/index.d.ts +41 -0
- package/types/source/source.ts +4 -0
- package/types/source/types.d.ts +67 -0
package/src/js/index.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
import querySelectorAll from 'shorter-js/src/selectors/querySelectorAll';
|
2
|
+
import ColorPicker from './color-picker';
|
3
|
+
|
4
|
+
function initCallBack() {
|
5
|
+
const { init, selector } = ColorPicker;
|
6
|
+
[...querySelectorAll(selector)].forEach(init);
|
7
|
+
}
|
8
|
+
|
9
|
+
if (document.body) initCallBack();
|
10
|
+
else document.addEventListener('DOMContentLoaded', initCallBack, { once: true });
|
11
|
+
|
12
|
+
export default ColorPicker;
|
@@ -0,0 +1,156 @@
|
|
1
|
+
/**
|
2
|
+
* A complete list of web safe colors.
|
3
|
+
* @see https://github.com/bahamas10/css-color-names/blob/master/css-color-names.json
|
4
|
+
* @type {string[]}
|
5
|
+
*/
|
6
|
+
const colorNames = [
|
7
|
+
'aliceblue',
|
8
|
+
'antiquewhite',
|
9
|
+
'aqua',
|
10
|
+
'aquamarine',
|
11
|
+
'azure',
|
12
|
+
'beige',
|
13
|
+
'bisque',
|
14
|
+
'black',
|
15
|
+
'blanchedalmond',
|
16
|
+
'blue',
|
17
|
+
'blueviolet',
|
18
|
+
'brown',
|
19
|
+
'burlywood',
|
20
|
+
'cadetblue',
|
21
|
+
'chartreuse',
|
22
|
+
'chocolate',
|
23
|
+
'coral',
|
24
|
+
'cornflowerblue',
|
25
|
+
'cornsilk',
|
26
|
+
'crimson',
|
27
|
+
'cyan',
|
28
|
+
'darkblue',
|
29
|
+
'darkcyan',
|
30
|
+
'darkgoldenrod',
|
31
|
+
'darkgray',
|
32
|
+
'darkgreen',
|
33
|
+
'darkgrey',
|
34
|
+
'darkkhaki',
|
35
|
+
'darkmagenta',
|
36
|
+
'darkolivegreen',
|
37
|
+
'darkorange',
|
38
|
+
'darkorchid',
|
39
|
+
'darkred',
|
40
|
+
'darksalmon',
|
41
|
+
'darkseagreen',
|
42
|
+
'darkslateblue',
|
43
|
+
'darkslategray',
|
44
|
+
'darkslategrey',
|
45
|
+
'darkturquoise',
|
46
|
+
'darkviolet',
|
47
|
+
'deeppink',
|
48
|
+
'deepskyblue',
|
49
|
+
'dimgray',
|
50
|
+
'dimgrey',
|
51
|
+
'dodgerblue',
|
52
|
+
'firebrick',
|
53
|
+
'floralwhite',
|
54
|
+
'forestgreen',
|
55
|
+
'fuchsia',
|
56
|
+
'gainsboro',
|
57
|
+
'ghostwhite',
|
58
|
+
'goldenrod',
|
59
|
+
'gold',
|
60
|
+
'gray',
|
61
|
+
'green',
|
62
|
+
'greenyellow',
|
63
|
+
'grey',
|
64
|
+
'honeydew',
|
65
|
+
'hotpink',
|
66
|
+
'indianred',
|
67
|
+
'indigo',
|
68
|
+
'ivory',
|
69
|
+
'khaki',
|
70
|
+
'lavenderblush',
|
71
|
+
'lavender',
|
72
|
+
'lawngreen',
|
73
|
+
'lemonchiffon',
|
74
|
+
'lightblue',
|
75
|
+
'lightcoral',
|
76
|
+
'lightcyan',
|
77
|
+
'lightgoldenrodyellow',
|
78
|
+
'lightgray',
|
79
|
+
'lightgreen',
|
80
|
+
'lightgrey',
|
81
|
+
'lightpink',
|
82
|
+
'lightsalmon',
|
83
|
+
'lightseagreen',
|
84
|
+
'lightskyblue',
|
85
|
+
'lightslategray',
|
86
|
+
'lightslategrey',
|
87
|
+
'lightsteelblue',
|
88
|
+
'lightyellow',
|
89
|
+
'lime',
|
90
|
+
'limegreen',
|
91
|
+
'linen',
|
92
|
+
'magenta',
|
93
|
+
'maroon',
|
94
|
+
'mediumaquamarine',
|
95
|
+
'mediumblue',
|
96
|
+
'mediumorchid',
|
97
|
+
'mediumpurple',
|
98
|
+
'mediumseagreen',
|
99
|
+
'mediumslateblue',
|
100
|
+
'mediumspringgreen',
|
101
|
+
'mediumturquoise',
|
102
|
+
'mediumvioletred',
|
103
|
+
'midnightblue',
|
104
|
+
'mintcream',
|
105
|
+
'mistyrose',
|
106
|
+
'moccasin',
|
107
|
+
'navajowhite',
|
108
|
+
'navy',
|
109
|
+
'oldlace',
|
110
|
+
'olive',
|
111
|
+
'olivedrab',
|
112
|
+
'orange',
|
113
|
+
'orangered',
|
114
|
+
'orchid',
|
115
|
+
'palegoldenrod',
|
116
|
+
'palegreen',
|
117
|
+
'paleturquoise',
|
118
|
+
'palevioletred',
|
119
|
+
'papayawhip',
|
120
|
+
'peachpuff',
|
121
|
+
'peru',
|
122
|
+
'pink',
|
123
|
+
'plum',
|
124
|
+
'powderblue',
|
125
|
+
'purple',
|
126
|
+
'rebeccapurple',
|
127
|
+
'red',
|
128
|
+
'rosybrown',
|
129
|
+
'royalblue',
|
130
|
+
'saddlebrown',
|
131
|
+
'salmon',
|
132
|
+
'sandybrown',
|
133
|
+
'seagreen',
|
134
|
+
'seashell',
|
135
|
+
'sienna',
|
136
|
+
'silver',
|
137
|
+
'skyblue',
|
138
|
+
'slateblue',
|
139
|
+
'slategray',
|
140
|
+
'slategrey',
|
141
|
+
'snow',
|
142
|
+
'springgreen',
|
143
|
+
'steelblue',
|
144
|
+
'tan',
|
145
|
+
'teal',
|
146
|
+
'thistle',
|
147
|
+
'tomato',
|
148
|
+
'turquoise',
|
149
|
+
'violet',
|
150
|
+
'wheat',
|
151
|
+
'white',
|
152
|
+
'whitesmoke',
|
153
|
+
'yellow',
|
154
|
+
'yellowgreen',
|
155
|
+
];
|
156
|
+
export default colorNames;
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import createElement from 'shorter-js/src/misc/createElement';
|
2
|
+
import setAttribute from 'shorter-js/src/attr/setAttribute';
|
3
|
+
import ariaLabelledBy from 'shorter-js/src/strings/ariaLabelledBy';
|
4
|
+
|
5
|
+
import vHidden from './vHidden';
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Returns a new color control `HTMLElement`.
|
9
|
+
* @param {number} iteration
|
10
|
+
* @param {number} id
|
11
|
+
* @param {number} width
|
12
|
+
* @param {number} height
|
13
|
+
* @param {string=} labelledby
|
14
|
+
* @returns {HTMLElement | Element}
|
15
|
+
*/
|
16
|
+
export default function getColorControl(iteration, id, width, height, labelledby) {
|
17
|
+
const labelID = `appearance${iteration}_${id}`;
|
18
|
+
const knobClass = iteration === 1 ? 'color-pointer' : 'color-slider';
|
19
|
+
const control = createElement({
|
20
|
+
tagName: 'div',
|
21
|
+
className: 'color-control',
|
22
|
+
});
|
23
|
+
setAttribute(control, 'role', 'presentation');
|
24
|
+
|
25
|
+
control.append(
|
26
|
+
createElement({
|
27
|
+
id: labelID,
|
28
|
+
tagName: 'label',
|
29
|
+
className: `color-label ${vHidden}`,
|
30
|
+
ariaLive: 'polite',
|
31
|
+
}),
|
32
|
+
createElement({
|
33
|
+
tagName: 'canvas',
|
34
|
+
className: `visual-control${iteration}`,
|
35
|
+
ariaHidden: 'true',
|
36
|
+
width: `${width}`,
|
37
|
+
height: `${height}`,
|
38
|
+
}),
|
39
|
+
);
|
40
|
+
|
41
|
+
const knob = createElement({
|
42
|
+
tagName: 'div',
|
43
|
+
className: `${knobClass} knob`,
|
44
|
+
});
|
45
|
+
setAttribute(knob, ariaLabelledBy, labelledby || labelID);
|
46
|
+
setAttribute(knob, 'tabindex', '0');
|
47
|
+
control.append(knob);
|
48
|
+
return control;
|
49
|
+
}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import createElement from 'shorter-js/src/misc/createElement';
|
2
|
+
import setAttribute from 'shorter-js/src/attr/setAttribute';
|
3
|
+
import ObjectAssign from 'shorter-js/src/misc/ObjectAssign';
|
4
|
+
import toUpperCase from 'shorter-js/src/misc/toUpperCase';
|
5
|
+
|
6
|
+
import vHidden from './vHidden';
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Returns the color form.
|
10
|
+
* @param {CP.ColorPicker} self the `ColorPicker` instance
|
11
|
+
* @returns {HTMLElement | Element}
|
12
|
+
*/
|
13
|
+
export default function getColorForm(self) {
|
14
|
+
const { format, id } = self;
|
15
|
+
const colorForm = createElement({
|
16
|
+
tagName: 'div',
|
17
|
+
className: `color-form ${format}`,
|
18
|
+
});
|
19
|
+
|
20
|
+
let components = ['hex'];
|
21
|
+
if (format === 'rgb') components = ['red', 'green', 'blue', 'alpha'];
|
22
|
+
else if (format === 'hsl') components = ['hue', 'saturation', 'lightness', 'alpha'];
|
23
|
+
|
24
|
+
components.forEach((c) => {
|
25
|
+
const [C] = format === 'hex' ? ['#'] : toUpperCase(c).split('');
|
26
|
+
const cID = `color_${format}_${c}_${id}`;
|
27
|
+
const cInputLabel = createElement({ tagName: 'label' });
|
28
|
+
setAttribute(cInputLabel, 'for', cID);
|
29
|
+
cInputLabel.append(
|
30
|
+
createElement({ tagName: 'span', ariaHidden: 'true', innerText: `${C}:` }),
|
31
|
+
createElement({ tagName: 'span', className: vHidden, innerText: `${c}` }),
|
32
|
+
);
|
33
|
+
const cInput = createElement({
|
34
|
+
tagName: 'input',
|
35
|
+
id: cID, // name: cID,
|
36
|
+
type: format === 'hex' ? 'text' : 'number',
|
37
|
+
value: c === 'alpha' ? '1' : '0',
|
38
|
+
className: `color-input ${c}`,
|
39
|
+
autocomplete: 'off',
|
40
|
+
spellcheck: 'false',
|
41
|
+
});
|
42
|
+
if (format !== 'hex') {
|
43
|
+
// alpha
|
44
|
+
let max = '1';
|
45
|
+
let step = '0.01';
|
46
|
+
if (c !== 'alpha') {
|
47
|
+
if (format === 'rgb') { max = '255'; step = '1'; } else if (c === 'hue') { max = '360'; step = '1'; } else { max = '100'; step = '1'; }
|
48
|
+
}
|
49
|
+
ObjectAssign(cInput, {
|
50
|
+
min: '0',
|
51
|
+
max,
|
52
|
+
step,
|
53
|
+
});
|
54
|
+
}
|
55
|
+
colorForm.append(cInputLabel, cInput);
|
56
|
+
});
|
57
|
+
return colorForm;
|
58
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import querySelectorAll from 'shorter-js/src/selectors/querySelectorAll';
|
2
|
+
import getDocument from 'shorter-js/src/get/getDocument';
|
3
|
+
import getDocumentBody from 'shorter-js/src/get/getDocumentBody';
|
4
|
+
|
5
|
+
import { addListener } from 'event-listener.js';
|
6
|
+
import ColorPicker from '../color-picker';
|
7
|
+
|
8
|
+
function initCallBack() {
|
9
|
+
const { init, selector } = ColorPicker;
|
10
|
+
[...querySelectorAll(selector)].forEach(init);
|
11
|
+
}
|
12
|
+
|
13
|
+
if (getDocumentBody()) initCallBack();
|
14
|
+
else addListener(getDocument(), 'DOMContentLoaded', initCallBack, { once: true });
|