-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
358 lines (336 loc) · 11.5 KB
/
script.js
File metadata and controls
358 lines (336 loc) · 11.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
var output = document.getElementById("code");
var color1 = document.getElementById("color1");
var color2 = document.getElementById("color2");
var body = document.getElementById("gradient");
var mouse = document.getElementById("mouse-icon");
var scrollWheel = document.getElementById("scroll-wheel");
var radioBtns = document.getElementsByClassName("radio");
var spans = document.getElementsByClassName('span');
var warnMsg = document.getElementById('warnMsg');
var deg = 90;
var pct = 50;
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var halfHeight = height/2;
var halfWidth = width/2;
// Attaching radio buttons to click listener
// These radio buttons change how many midpoints we have in our gradient
for(let i = 0; i < radioBtns.length; i++){
radioBtns[i].addEventListener('click', colorChanged)
if(i === 2){
radioBtns[i].addEventListener('click', showMsg)
} else {
radioBtns[i].addEventListener('click', hideMsg)
}
}
//When window resized, adjust client height and width variables.
var resizeEvent = function(){
width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
halfHeight = height/2;
halfWidth = width/2;
}
window.addEventListener("resize", resizeEvent);
//Setting up Color pickers
const pickr1 = new Pickr({
el: '.color-picker1',
default: 'rgb(209, 122, 255)',
comparison: true,
useAsButton: true,
defaultRepresentation: 'HEX',
components: {
preview: true,
opacity: true,
hue: true,
interaction: {
hex: true,
rgba: true,
hsva: true,
input: true,
clear: false,
save: true,
}
}
});
const pickr2 = new Pickr({
el: '.color-picker2',
comparison: true,
useAsButton: true,
default: 'rgb(94, 255, 168)',
defaultRepresentation: 'HEX',
components: {
preview: true,
opacity: true,
hue: true,
interaction: {
hex: true,
rgba: true,
hsva: true,
input: true,
clear: false,
save: true
}
}
});
//Attaching the change listeners to the colour pickers
//These will change the background gradient color and button color live
pickr1.on('change', (...args) => {
const {toHEX} = args[0]
color1.value = args[0].toHEX();
color1.style.background = args[0].toHEX();
colorChanged()
});
pickr2.on('change', (...args) => {
const {toHEX} = args[0]
color2.value = args[0].toHEX();
color2.style.background = args[0].toHEX();
colorChanged()
});
//Show the warn message if we are on the third midpoint setting
//pct% functionality on third setting is disabled
function showMsg(){ warnMsg.style.opacity = '1'}
function hideMsg(){ warnMsg.style.opacity = '0'}
//Change gradient color when a new color has been selected
function colorChanged(){
body.style.background = `linear-gradient( ${deg}deg, ${color1.value}, ${pct}%, ${color2.value} )`;
//If more than one midpoint is selected
if(!radioBtns[0].checked){ getMidPoints() }
//Set css output text
output.textContent = "background: " + body.style.background;
//change textColor
changeTextColor();
}
//Calculates the mid point color codes
function getMidPoints(){
//Finding first midpoint between two colours chosen
let [r,g,b] = emphasis_diminish(
((parseInt("0x" + color1.value[1] + color1.value[2]) + parseInt("0x" + color2.value[1] + color2.value[2])) / 2),
((parseInt("0x" + color1.value[3] + color1.value[4]) + parseInt("0x" + color2.value[3] + color2.value[4])) / 2),
((parseInt("0x" + color1.value[5] + color1.value[6]) + parseInt("0x" + color2.value[5] + color2.value[6])) / 2),
0.3
)
//only one midpoint
if(radioBtns[1].checked){
let pct1 = (pct - 33) < 0 ? 1 : (pct - 33)
let pct2 = pct < 33 ? 34 : pct
body.style.background = `linear-gradient( ${deg}deg, ${color1.value}, ${pct1}%, rgb(${r},${g},${b}), ${pct2}%, ${color2.value})`;
// if(pct2 < 33.3){
// body.style.background = `linear-gradient( ${deg}deg, ${color1.value}, 0.1%, rgb(${r},${g},${b}), 33.3%, ${color2.value})`;
// }
} //Three midpoints, calculate between each colour and the last mid point
else if(radioBtns[2].checked){
let [r2, g2, b2] = emphasis_diminish(
(r + (parseInt("0x" + color1.value[1] + color1.value[2]))) /2,
(g + (parseInt("0x" + color1.value[3] + color1.value[4]))) /2,
(b + (parseInt("0x" + color1.value[5] + color1.value[6]))) /2,
0.1
)
let [r3, g3, b3] = emphasis_diminish(
(r + (parseInt("0x" + color2.value[1] + color2.value[2]))) /2,
(g + (parseInt("0x" + color2.value[3] + color2.value[4]))) /2,
(b + (parseInt("0x" + color2.value[5] + color2.value[6]))) /2,
0.1
)
body.style.background = `linear-gradient( ${deg}deg, ${color1.value}, rgb(${r2},${g2},${b2}), rgb(${r},${g},${b}), rgb(${r3},${g3},${b3}), ${color2.value} )`;
}
}
//This function changes the text color depending on how dark/light it is
function changeTextColor(){
let total1 = 0
let total2 = 0
//Calculate the total RGB value
for(let i = 1; i < 7; i+=2){
total1 += parseInt("0x" + color1.value[i] + color1.value[i+1])
total2 += parseInt("0x" + color2.value[i] + color2.value[i+1])
}
//If total is above 375(half of max) for one color then its too bright, change to black text
//else it is too dark, change to white
if(total1 > 375){
color1.style.color = 'black'
} else {
color1.style.color = 'white'
}
if(total2 > 375){
color2.style.color = "black"
}else {
color2.style.color = 'white'
}
//Same but for whole body text content, add up total for both colors and calculate
//quite a few things to change here.
if(total1 + total2 > 750){
mouse.style.border = '2px solid black'
body.style.color = 'black'
scrollWheel.style.background = 'black'
output.style.borderLeft =" 3px solid rgba(0,0,0, 0.5)";
output.style.borderRight =" 3px solid rgba(0,0,0, 0.5)";
for(let i = 0; i < radioBtns.length; i++){
radioBtns[i].style.border = '2px solid black'
}
for(let i = 0; i < spans.length; i++){
spans[i].style.color = 'black'
}
} else {
mouse.style.border = '2px solid white'
body.style.color = 'white'
scrollWheel.style.background = 'white'
output.style.borderLeft =" 3px solid rgba(255,255,255, 0.5)";
output.style.borderRight =" 3px solid rgba(255,255,255, 0.5)";
for(let i = 0; i < radioBtns.length; i++){
radioBtns[i].style.border = '2px solid white'
}
for(let i = 0; i < spans.length; i++){
spans[i].style.color = 'white'
}
}
}
// Function to emphasis the strongest colours and take away the weakest
// i thought this would be a good idea to add a bit more flavor to the colors.
// It seems to work ok. Simple formula
function emphasis_diminish(r,g,b, p){
let max = Math.max(r,g,b)
//if it is close to a shade of gray, do not add color
if(r >= b - 5 && r <= b +5 && r >= g - 5 && r <= g + 5 && g >= b - 5 && g <= b + 5){
return [r,g,b];
}
//Finds the brightest colour and emphasis's it, dim's the dimmest
if(max == r){
r = r * (1+p);
if(Math.max(b,g) == b){
g = g * (1-p)
} else {
b = b * (1-p)
}
} else if(max == b){
b = b * (1+p)
if(Math.max(r, g) == r){
g = g * (1-p)
} else {
r = r * (1-p)
}
} else {
g = g * (1+p)
if(Math.max(r,b) == r){
b = b * (1-p)
} else {
r = r * (1-p)
}
}
//Making sure no value it too big or in negative
r = r > 255 ? 255 : r
b = b > 255 ? 255 : b
g = g > 255 ? 255 : g
//Taking away decimal places
r = Math.trunc(r)
b = Math.trunc(b)
g = Math.trunc(g)
return [r,g,b]
}
dragElement(mouse);
//When mouse dragged, get the element and the cursor postion (x,y)
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
// if present, the header is where you move the DIV from:
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
// otherwise, move the DIV from anywhere inside the DIV:
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
elmnt.classList.toggle("mouse-transition");
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
// This gets called every 0.1 seconds, finds the new cursor position and draws
// calls the function to draw the gradient
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (pos4) + "px";
elmnt.style.left = (pos3) + "px";
// Draw the background gradient with the new parameters
drawScreen(pos3, pos4);
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
elmnt.classList.toggle("mouse-transition");
}
}
//This method draws the gradient by calculating two values
// 1: Angle of gradient
// Using pythagorean theorem to create a triangle and then triganometry to find the angle
// center *
// angle -> | \
// | \
// | \
// side a | \ side c
// | \
// |______\ <- element position
// side b
// side a is calculated as the difference between center and elements y-value
// side b is calculated as the difference betweeen centet and elements x-value
// side c is calulated using pythag. c^2 = a^2 + b^2
// angle is calculated using trig
// 2: Percentage of color 1 and color 2
// this is found by calculating how far the draggable element is away from the
// center (original location of element) as opposed to the nearest edge of screen
// e.g. all the way to the left is 100%, center is 0% and between left and center is 50%
function drawScreen(x, y){
//Finding the three sides
let [a,b,c] = createTriangle(x,y);
// Calculating percentage
pct = percentageFromCenter(a, b);
//Calculation is slightly different for each half of the screen
if(x > halfWidth){
//Trig to find angle
deg = trigCosA(a,b,c);
// The degree in linear-gradient go -180 to 180 so here we use -90 to 90
if(y < halfHeight){
deg = 0 - deg;
}
}
else {
//Trig to find angle
deg = trigCosA(b, a,c);
// The degree in linear-gradient go -180 to 180 so here we use -180 to -90 and 90 to 180
if(y > halfHeight){
deg += 90;
}
else{
deg = -90 - deg;
}
}
deg = deg.toFixed(1);
colorChanged();
}
function trigCosA(a,b,c){
let cosA = (b*b + c*c - a*a) / (2*b*c);
cosA = Math.acos(cosA);
return cosA*180/Math.PI;
}
function percentageFromCenter(a,b){
let pctY = (a / halfHeight) * 100;
let pctX = (b / halfWidth) * 100;
return Math.max(pctY, pctX).toFixed(1);
}
function createTriangle(x, y){
a = Math.abs(halfHeight - y);
b = Math.abs(halfWidth - x);
//Pythag to find c
c = Math.sqrt(a*a + b*b);
return[a,b,c];
}