回答編集履歴

2

修正

2019/10/11 08:28

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア36115

test CHANGED
@@ -120,9 +120,7 @@
120
120
 
121
121
  el.style.backgroundColor = el.value;
122
122
 
123
- let color = pickr.getColor();
124
-
125
- el.style.color = blackOrWhite( color.toRGBA() );
123
+ el.style.color = blackOrWhite( pickr.getColor().toRGBA() );
126
124
 
127
125
 
128
126
 

1

コメントを受けて追記

2019/10/11 08:28

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア36115

test CHANGED
@@ -3,3 +3,151 @@
3
3
 
4
4
 
5
5
  [Pickr fiddle template - JSFiddle](https://jsfiddle.net/Simonwep/wL1zyqcd/)
6
+
7
+
8
+
9
+
10
+
11
+ # コメントを受けて追記
12
+
13
+
14
+
15
+ こんな感じでしょうか?
16
+
17
+ [https://jsfiddle.net/Lhankor_Mhy/yjdtch9n/](https://jsfiddle.net/Lhankor_Mhy/yjdtch9n/)
18
+
19
+ ```html
20
+
21
+ <section class="container">
22
+
23
+ <input type="text" name="test" id="test1" value="#000">
24
+
25
+ <input type="text" name="test" id="test2" value="#8BC34AD9">
26
+
27
+ <input type="text" name="test" id="test2" value="red">
28
+
29
+ <input type="text" name="test" id="test2" value="brown">
30
+
31
+ <input type="text" name="test" id="test3" value="white">
32
+
33
+ </section>
34
+
35
+ ```
36
+
37
+ ```js
38
+
39
+ const container = document.querySelectorAll('.container>input').forEach( el => {
40
+
41
+
42
+
43
+ const pickr = new Pickr({
44
+
45
+ el,
46
+
47
+ useAsButton: true,
48
+
49
+ default: '#42445A',
50
+
51
+ theme: 'classic',
52
+
53
+
54
+
55
+ swatches: [
56
+
57
+ 'rgba(244, 67, 54, 1)',
58
+
59
+ 'rgba(233, 30, 99, 0.95)',
60
+
61
+ 'rgba(156, 39, 176, 0.9)',
62
+
63
+ 'rgba(103, 58, 183, 0.85)',
64
+
65
+ 'rgba(63, 81, 181, 0.8)',
66
+
67
+ 'rgba(33, 150, 243, 0.75)',
68
+
69
+ 'rgba(3, 169, 244, 0.7)',
70
+
71
+ 'rgba(0, 188, 212, 0.7)',
72
+
73
+ 'rgba(0, 150, 136, 0.75)',
74
+
75
+ 'rgba(76, 175, 80, 0.8)',
76
+
77
+ 'rgba(139, 195, 74, 0.85)',
78
+
79
+ 'rgba(205, 220, 57, 0.9)',
80
+
81
+ 'rgba(255, 235, 59, 0.95)',
82
+
83
+ 'rgba(255, 193, 7, 1)'
84
+
85
+ ],
86
+
87
+
88
+
89
+ components: {
90
+
91
+ preview: true,
92
+
93
+ opacity: true,
94
+
95
+ hue: true,
96
+
97
+
98
+
99
+ interaction: {
100
+
101
+ hex: true,
102
+
103
+ rgba: true,
104
+
105
+ hsva: true,
106
+
107
+ input: true,
108
+
109
+ clear: true,
110
+
111
+ save: true
112
+
113
+ }
114
+
115
+ }
116
+
117
+ }).on('init', pickr => {
118
+
119
+ pickr.setColor(el.value);
120
+
121
+ el.style.backgroundColor = el.value;
122
+
123
+ let color = pickr.getColor();
124
+
125
+ el.style.color = blackOrWhite( color.toRGBA() );
126
+
127
+
128
+
129
+ }).on('save', color => {
130
+
131
+ el.value = color.toHEXA().toString(0);
132
+
133
+ el.style.backgroundColor = el.value;
134
+
135
+ el.style.color = blackOrWhite( color.toRGBA() );
136
+
137
+ pickr.hide();
138
+
139
+ });
140
+
141
+
142
+
143
+ });
144
+
145
+ function blackOrWhite ( [r, g, b] ) {
146
+
147
+ // via. https://lab.syncer.jp/Web/JavaScript/Snippet/55/
148
+
149
+ return ( ( ( (r * 299) + (g * 587) + (b * 114) ) / 1000 ) < 128 ) ? "white" : "black" ;
150
+
151
+ }
152
+
153
+ ```