-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathcolor.js
More file actions
226 lines (189 loc) · 6.78 KB
/
color.js
File metadata and controls
226 lines (189 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import $ from 'blingblingjs'
import { TinyColor } from '@ctrl/tinycolor'
import { getStyle } from '../utilities/'
import { handleEditEvent } from "./events";
const state = {
active_color: 'background',
elements: [],
}
export function ColorPicker(pallete, selectorEngine) {
const foregroundPicker = $('#foreground', pallete)
const backgroundPicker = $('#background', pallete)
const borderPicker = $('#border', pallete)
const fgInput = $('input', foregroundPicker[0])
const bgInput = $('input', backgroundPicker[0])
const boInput = $('input', borderPicker[0])
const shadows = {
active: '0 0 0 2px hotpink, rgba(0, 0, 0, 0.25) 0px 0.25em 0.5em',
inactive: '0 0 0 2px var(--theme-bg), rgba(0, 0, 0, 0.25) 0px 0.25em 0.5em',
}
const state = {
active_color: undefined,
elements: [],
}
fgInput.on('input', ({ target: { value } }) => {
state.elements.map(el => {
const oldStyle = el.style["color"];
el.style['color'] = value
handleEditEvent({
el,
editType: "STYLE",
newValue: { color: value },
oldValue: { color: oldStyle },
});
})
foregroundPicker[0].style.setProperty(`--contextual_color`, value)
})
bgInput.on('input', ({ target: { value } }) => {
state.elements.map(el => {
const oldStyle = el.style[el instanceof SVGElement ? "fill" : "backgroundColor"];
el.style[el instanceof SVGElement ? 'fill' : 'backgroundColor'] = value
handleEditEvent({
el,
editType: "STYLE",
newValue: { [el instanceof SVGElement ? "fill" : "backgroundColor"]: value },
oldValue: { [el instanceof SVGElement ? "fill" : "backgroundColor"]: oldStyle },
});
})
backgroundPicker[0].style.setProperty(`--contextual_color`, value)
})
boInput.on('input', ({ target: { value } }) => {
state.elements.map(el => {
const oldStyle =
el.style[el instanceof SVGElement ? "fill" : "backgroundColor"];
el.style[el instanceof SVGElement
? 'stroke'
: 'borderColor'
] = value
handleEditEvent({
el,
editType: "STYLE",
newValue: { [el instanceof SVGElement ? "fill" : "backgroundColor"]: value },
oldValue: { [el instanceof SVGElement ? "fill" : "backgroundColor"]: oldStyle },
});
});
borderPicker[0].style.setProperty(`--contextual_color`, value)
})
const extractColors = elements => {
state.elements = elements
let isMeaningfulForeground = false
let isMeaningfulBackground = false
let isMeaningfulBorder = false
let FG, BG, BO
if (state.elements.length == 1) {
const el = state.elements[0]
if (el instanceof SVGElement) {
FG = new TinyColor('rgb(0, 0, 0)')
var bo_temp = getStyle(el, 'stroke')
BO = new TinyColor(bo_temp === 'none'
? 'rgb(0, 0, 0)'
: bo_temp)
BG = new TinyColor(getStyle(el, 'fill'))
}
else {
FG = new TinyColor(getStyle(el, 'color'))
BG = new TinyColor(getStyle(el, 'backgroundColor'))
BO = getStyle(el, 'borderWidth') === '0px'
? new TinyColor('rgb(0, 0, 0)')
: new TinyColor(getStyle(el, 'borderColor'))
}
let fg = FG.toHslString()
let bg = BG.toHslString()
let bo = BO.toHslString()
isMeaningfulForeground = FG.originalInput !== 'rgb(0, 0, 0)' || (el.children.length === 0 && el.textContent !== '')
isMeaningfulBackground = BG.originalInput !== 'rgba(0, 0, 0, 0)'
isMeaningfulBorder = BO.originalInput !== 'rgb(0, 0, 0)'
if (isMeaningfulForeground && !isMeaningfulBackground)
setActive('foreground')
else if (isMeaningfulBackground && !isMeaningfulForeground || isMeaningfulBackground && isMeaningfulForeground)
setActive('background')
const new_fg = isMeaningfulForeground ? fg : ''
const new_bg = isMeaningfulBackground ? bg : ''
const new_bo = isMeaningfulBorder ? bo : ''
const fg_icon = isMeaningfulForeground ? healthyContrastColor(FG) : ''
const bg_icon = isMeaningfulBackground ? healthyContrastColor(BG) : ''
const bo_icon = isMeaningfulBorder ? healthyContrastColor(BO) : ''
fgInput.attr('value', `#` + FG.toHex())
bgInput.attr('value', `#` + BG.toHex())
boInput.attr('value', `#` + BO.toHex())
foregroundPicker.attr('style', `
--contextual_color: ${new_fg};
--icon_color: ${fg_icon};
`)
backgroundPicker.attr('style', `
--contextual_color: ${new_bg};
--icon_color: ${bg_icon};
`)
borderPicker.attr('style', `
--contextual_color: ${new_bo};
--icon_color: ${bo_icon};
`)
}
else {
// show all 3 if they've selected more than 1 node
// todo: this is giving up, and can be solved
foregroundPicker.attr('style', `
box-shadow: ${state.active_color == 'foreground' ? shadows.active : shadows.inactive};
--contextual_color: transparent;
--icon_color: hsla(0,0%,0%,80%);
`)
backgroundPicker.attr('style', `
box-shadow: ${state.active_color == 'background' ? shadows.active : shadows.inactive};
--contextual_color: transparent;
--icon_color: hsla(0,0%,0%,80%);
`)
borderPicker.attr('style', `
box-shadow: ${state.active_color == 'border' ? shadows.active : shadows.inactive};
--contextual_color: transparent;
--icon_color: hsla(0,0%,0%,80%);
`)
}
}
const getActive = () =>
state.active_color
const setActive = key => {
removeActive()
state.active_color = key
if (key === 'foreground')
foregroundPicker[0].style.boxShadow = shadows.active
if (key === 'background')
backgroundPicker[0].style.boxShadow = shadows.active
if (key === 'border')
borderPicker[0].style.boxShadow = shadows.active
}
const removeActive = () =>
[foregroundPicker, backgroundPicker, borderPicker].forEach(([picker]) =>
picker.style.boxShadow = shadows.inactive)
selectorEngine.onSelectedUpdate(extractColors)
return {
getActive,
setActive,
foreground: {
color: color =>
foregroundPicker[0].style.setProperty('--contextual_color', color)
},
background: {
color: color =>
backgroundPicker[0].style.setProperty('--contextual_color', color)
}
}
}
export const healthyContrastColor = color => {
let contrast = color.clone()
contrast = contrast.isDark()
? contrast.lighten(75)
: contrast.darken(50)
return contrast.toHslString()
}
export const functionalNotate = color => {
const chunks = color.split(',')
if (chunks.length === 4) {
let opacity = chunks.pop()
chunks[0] = chunks[0].replace('hsla', 'hsl')
chunks[0] = chunks[0].replace('rgba', 'rgb')
return chunks.join(' ') + ` / ${opacity}`
}
else {
return chunks.join(' ')
}
}