-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
150 lines (117 loc) · 3.86 KB
/
script.js
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
let pallettes = localStorage.getItem('palletes')
if (!pallettes) {
localStorage.setItem('palletes', JSON.stringify([]))
}
const cols = document.querySelectorAll('.col')
document.addEventListener('keydown', event => {
event.preventDefault()
if (event.code.toLowerCase() === 'space'){
setRandomColors()
}
})
document.addEventListener('click', event => {
const type = event.target.dataset.type
if (type === 'lock') {
const node = event.target.tagName.toLowerCase() === 'i'
? event.target
: event.target.children[0]
node.classList.toggle('fa-lock-open')
node.classList.toggle('fa-lock')
} else if (type === 'copy') {
copyToClickboard(event.target.textContent)
} else if (type === 'save') {
const node = event.target.tagName.toLowerCase() === 'i'
? event.target
: event.target.children[0]
node.classList.toggle('fa-regular')
node.classList.toggle('fa-solid')
let pallettes = localStorage.getItem('palletes')
pallettes = JSON.parse(pallettes)
if (document.location.hash) {
if (pallettes.indexOf(document.location.hash) === -1) {
pallettes.unshift(document.location.hash)
} else {
pallettes.splice(pallettes.indexOf(document.location.hash), 1)
}
localStorage.setItem('palletes', JSON.stringify(pallettes))
}
}
} )
function gerenerateRandomColor() {
// RGB
// #FF0000
// #00FF00
// #0000FF
const hexCodes ='123456789ABCDEF'
let color = ''
for (let i = 0; i < 6; i++) {
color += hexCodes[Math.floor(Math.random() * hexCodes.length)]
}
return '#' + color
}
function copyToClickboard(text) {
return navigator.clipboard.writeText(text)
}
function setRandomColors(isInitial){
const colors = isInitial ? getColorsFromHash() : []
cols.forEach((col, index) => {
const isLocked = col.querySelector('i').classList.contains('fa-lock')
const text =col.querySelector('h2')
const button =col.querySelector('button')
const book =col.querySelector('.fa-bookmark')
const pallette =col.querySelector('.fa-palette')
const lock = col.querySelector('.fa-solid')
if (isLocked) {
colors.push(text.textContent)
return
}
let allPallettes = localStorage.getItem('palletes')
allPallettes = JSON.parse(pallettes)
if (allPallettes) {
if (allPallettes.indexOf(document.location.hash) !== -1 && book) {
book.classList.toggle('fa-solid')
book.classList.toggle('fa-regular')
} else if (allPallettes.indexOf(document.location.hash) === -1 && book) {
book.classList.remove('fa-regular')
book.classList.remove('fa-solid')
book.classList.add('fa-regular')
}
}
const color = isInitial
? colors[index]
? colors[index]
:chroma.random()
: chroma.random()
colors.push(color)
text.textContent = color
col.style.background = color
setTextColor(lock, color)
setTextColor(text, color)
setTextColor(button, color)
if (book && pallette) {
setTextColor(book, color)
setTextColor(pallette, color)
}
})
updateColorsHash(colors)
}
function setTextColor (text, color) {
const luminance = chroma(color).luminance()
text.style.color = luminance > 0.5 ? 'black' : 'white'
}
function updateColorsHash(colors = []) {
document.location.hash = colors
.map((col) => {
return col.toString().substring(1)
}).join('-')
}
function getColorsFromHash() {
if (document.location.hash.length > 1) {
return document.location.hash
.substring(1)
.split('-')
.map(color => '#' + color)
}
return []
}
setRandomColors(true)