-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
276 lines (226 loc) · 7.69 KB
/
index.js
File metadata and controls
276 lines (226 loc) · 7.69 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// TODO
// Adds listeners to every input element in the form
// consider wrapping the outputs on seperate divs like from the form? (.form-control)
// on mobile and tablet, outputs are put in a modal and would be triggered when button "info" is clicked
const col = document.querySelector('#color');
const theorySelect = document.querySelector('#theorySelect')
const section = document.querySelector('.cont')
const info = document.querySelector('svg')
const modal = document.querySelector('#modal')
const hexColourIn = document.querySelector('#hexColour')
const rgbColourIn = document.querySelector('#rgbColour')
const hslColourIn = document.querySelector('#hslColour')
const baseDiv = document.querySelector('#base')
const secDiv = section.querySelector('#secDiv')
const thirdDiv = document.createElement('div')
thirdDiv.classList.add('w-100', 'h-100', 'd-flex', 'flex-column')
thirdDiv.setAttribute('id', 'thirdDiv')
window.onload = function () {
color.value = '#007FFF'
// Fires off the event to apply the colour theory on default
changeTheory(theorySelect.value)
}
// LISTENERS
col.addEventListener('input', () => {
const theory = theorySelect.value
changeBgColor(theory)
})
theorySelect.addEventListener('input', (e) => {
const theory = e.target.value
changeTheory(theory)
})
info.addEventListener('click', () => {
modal.classList.add('d-flex')
modal.addEventListener('click', () => {
modal.classList.remove('d-flex')
})
})
// FUNCTIONS
function changeTheory(theory) {
if (theory == 'com') {
if (section.children.length === 3) {
section.removeChild(section.lastElementChild)
}
// Fires off the event to apply the complement colour theory
changeBgColor(theory)
}
else {
section.appendChild(thirdDiv)
// Fires off the event to apply the analogous colour theory
changeBgColor(theory)
}
}
function changeBgColor(theory) {
// converts to different color definitions
const hexColor = col.value
const { r, g, b } = hexToRgb(hexColor)
const { h, s, l } = rgb2hsl(r, g, b)
// fills the input text with converted values
hexColourIn.value = hexColor.toUpperCase()
rgbColourIn.value = `rgb(${r}, ${g}, ${b})`
hslColourIn.value = `hsl(${h}, ${s}%, ${l}%)`
// if Complementary Theory
if (theory === 'com') {
baseDiv.style.backgroundColor = hexColor;
// clears existing headings
clearHeadings()
// adds new heading to the baseColor div
shouldUseWhiteFont(baseDiv)
baseDiv.appendChild(addsBaseHeading())
// compputes the complementary colour of the chosen base colour
const computedCompHue = computeComplementary(h)
// changes the bgcolor of the div to the computed hsl colour
secDiv.style.backgroundColor = `hsl(${computedCompHue}, ${s}%, ${l}%)`
// adds heading to the secondary div
shouldUseWhiteFont(secDiv)
secDiv.appendChild(addsCompHeading())
appendColourInfo(secDiv)
}
// Analogous Theory
else {
// the secDiv becomes the baseDiv to put the base colour in the middle of the page
secDiv.style.backgroundColor = hexColor;
// clears existing headings
clearHeadings()
// adds new heading to highlight the baseColour div
shouldUseWhiteFont(secDiv)
secDiv.appendChild(addsBaseHeading())
// computes the analogous values according to basecolour the assigns to appropriate divs
const { first, second } = computeAnalogous(h)
baseDiv.style.backgroundColor = `hsl(${first}, ${s}%, ${l}%)`
shouldUseWhiteFont(baseDiv)
baseDiv.appendChild(addsAnHeading())
appendColourInfo(baseDiv)
thirdDiv.style.backgroundColor = `hsl(${second}, ${s}%, ${l}%)`
shouldUseWhiteFont(thirdDiv)
thirdDiv.appendChild(addsAnHeading())
appendColourInfo(thirdDiv)
}
}
function appendColourInfo(element) {
const div = document.createElement('div')
div.classList.add('d-flex', 'flex-column', 'justify-content-center', 'align-items-center', 'info-div')
// element's rgb value
const [r, g, b] = extractRGBfromString(element)
// element's hex value added to the DOM
const hex = rgbToHex(r, g, b).toUpperCase()
const hexLabel = document.createElement('label')
const hexVal = document.createElement('input')
hexLabel.textContent = "Hex Colour"
hexVal.value = hex
hexVal.readOnly = true;
hexVal.classList.add('form-control-lg', 'hex-output')
div.append(hexLabel, hexVal)
// element's rgb value added to the DOM
const rgbLabel = document.createElement('label')
const rgbVal = document.createElement('input')
rgbLabel.textContent = "RGB Colour"
rgbVal.value = `rgb(${r}, ${g}, ${b})`
rgbVal.readOnly = true;
rgbVal.classList.add('form-control-lg', 'rgb-output')
div.append(rgbLabel, rgbVal)
// element's hsl value
const { h, s, l } = rgb2hsl(r, g, b)
const hslLabel = document.createElement('label')
const hslVal = document.createElement('input')
hslLabel.textContent = "HSL Colour"
hslVal.value = `hsl(${h}, ${s}%, ${l}%)`
hslVal.readOnly = true;
hslVal.classList.add('form-control-lg', 'hsl-output')
div.append(hslLabel, hslVal)
element.append(div)
}
// algo to find the optimum colour of heading to maximise contrast
function shouldUseWhiteFont(element) {
// element.style.backgroundColor returns a string in rgb format then r,g,b are extracted
const [r, g, b] = extractRGBfromString(element)
const isWhite = (r * 0.299 + g * 0.587 + b * 0.114) < 186
isWhite ? element.style.color = "white" : element.style.color = "#333333"
}
function clearHeadings() {
baseDiv.innerHTML = ""
secDiv.innerHTML = ""
thirdDiv.innerHTML = ""
}
function addsBaseHeading() {
const h2 = document.createElement('h2')
h2.textContent = "Base Colour"
h2.classList.add('pt-3')
return h2
}
function addsCompHeading() {
const h2 = document.createElement('h2')
h2.textContent = "Complementary Colour"
h2.classList.add('pt-3')
return h2
}
function addsAnHeading() {
const h2 = document.createElement('h2')
h2.textContent = "Analogous Colour"
h2.classList.add('pt-3')
return h2
}
// returns an array with the r,g,b values from an element
function extractRGBfromString(element) {
const rgb = element.style.backgroundColor
var matchColors = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
var match = matchColors.exec(rgb);
return match.slice(1)
}
// COLOR THEORIES ALGO
function computeComplementary(hue) {
return (hue + 180) % 360
}
function computeAnalogous(hue) {
return { first: (hue + 30) % 360, second: ((hue - 30) + 360) % 360 }
}
// COLOR CONVERSION ALGOS
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function rgbToHex(r, g, b) {
// return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
return '#' + [parseInt(r), parseInt(g), parseInt(b)].map(x => {
const hex = x.toString(16)
return hex.length === 1 ? '0' + hex : hex
}).join('')
}
function rgb2hsl(r, g, b) {
// Make r, g, and b fractions of 1
r /= 255;
g /= 255;
b /= 255;
// Find greatest and smallest channel values
let cmin = Math.min(r, g, b),
cmax = Math.max(r, g, b),
delta = cmax - cmin,
h = 0,
s = 0,
l = 0;
if (delta == 0) h = 0;
else if (cmax == r)
// Red is max
h = ((g - b) / delta) % 6;
else if (cmax == g)
// Green is max
h = (b - r) / delta + 2;
else
// Blue is max
h = (r - g) / delta + 4;
h = Math.round(h * 60);
// Make negative hues positive behind 360°
if (h < 0) h += 360;
// Calculate lightness
l = (cmax + cmin) / 2;
// Calculate saturation
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
// Multiply l and s by 100
s = +(s * 100).toFixed(1);
l = +(l * 100).toFixed(1);
return { h, s, l }
}