Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (165 loc) · 6.3 KB
/
Copy pathindex.js
File metadata and controls
185 lines (165 loc) · 6.3 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
document.addEventListener('DOMContentLoaded', () => {
// const storedColors = localStorage.getItem('colors');
// if (storedColors) {
// //if the colors are already stored, apply it the same way getColor() worked
// const colors = JSON.parse(storedColors)
// let swatch = "swatch";
// let hex = "hex";
// let swatchNum = 1;
// for (const elem of colors) {
// let concatSwatch = swatch + swatchNum;
// const colorSwatch = document.querySelector(`.${concatSwatch}`);
// colorSwatch.style.backgroundColor = `${elem}`;
// let concatHex = hex + swatchNum;
// const hexNum = document.querySelector(`.${concatSwatch}`);
// hexNum.textContent = `${elem}`;
// swatchNum++;
// }
// }
// chrome.storage.local.get(["colors"]).then((result) => {
// if(result.colors) {
// } else {
// getColor()
// }
// });
getColor(); //otherwise randomize a new set of colors
});
// function getColor() {
// fetch(`https://www.thecolorapi.com/id?hex=${randomColor()}`,
// {headers: {
// "Accept": "application/json"
// }}
// )
// .then((response) => {
// if(!response.ok) throw new Error(`${response.status}`);
// return response.json();
// })
// .then((data) => {
// console.log(data);
// // const genDiv = document.getElementById('genDiv');
// // const hexData = document.createElement('h4');
// // hexData.innerHTML = data.hex.value;
// const hexNum = document.getElementById('hex');
// hexNum.textContent = data.hex.value;
// // hexNum.appendChild(hexNum);
// const colorDisplay = document.getElementById('color-display');
// colorDisplay.style.backgroundColor = `${data.hex.value}`
// })
// .catch((error) => console.error('Fetch Error:', error));
// }
function getColor() {
fetch(
`https://www.thecolorapi.com/scheme?hex=${randomColor()}&mode=analogic&count=6`,
{
headers: {
Accept: 'application/json',
},
}
)
.then((response) => {
if (!response.ok) throw new Error(`${response.status}`);
return response.json();
})
.then((data) => {
console.log(data);
const colors = data.colors;
let swatch = "swatch";
let hex = "hex";
let swatchNum = 1;
const array = [];
for (const color of colors) {
let concatSwatch = swatch + swatchNum;
console.log(concatSwatch);
const colorSwatch = document.querySelector(`.${concatSwatch}`);
colorSwatch.style.backgroundColor = `${color.hex.value}`;
// const hexVal = document.createElement('p');
// hexVal.setAttribute('id','hexvalue');
let concatHex = hex + swatchNum;
const hexNum = document.querySelector(`.${concatHex}`);
hexNum.textContent = `${color.hex.value}`;
swatchNum++;
// colorSwatches.appendChild(colorDisplay);
// const breakElem = document.createElement('br');
// colorSwatches.appendChild(breakElem);
array.push(`${color.hex.value}`);
console.log(array);
}
// const hexNum = document.getElementById('hex');
// hexNum.textContent = data.hex.value;
// const colorDisplay = document.getElementById('color-display');
// colorDisplay.style.backgroundColor = `${data.hex.value}`
// localStorage.setItem('colors',JSON.stringify(array))
})
.catch((error) => console.error('Fetch Error:', error));
}
// function displayPalette(color) {
// // const hexNum = document.getElementById('hex');
// // hexNum.textContent = data.hex.value;
// // const colorDisplay = document.getElementById('color-display');
// // colorDisplay.style.backgroundColor = `${data.hex.value}`
// const colorSwatches = document.getElementById('color-swatches');
// const colorDisplay = document.createElement('div');
// colorDisplay.id = 'color-display';
// colorDisplay.style.backgroundColor = `${color.hex.value}`;
// colorSwatches.appendChild(colorDisplay);
// const breakElem = document.createElement('br');
// colorSwatches.appendChild(breakElem);
// }
const randomColor = () => {
const red = Math.floor(Math.random() * 255).toString(16);
const green = Math.floor(Math.random() * 255).toString(16);
const blue = Math.floor(Math.random() * 255).toString(16);
// let red = '5'
// let green = '7'
// let blue = 'cb'
let colors = [red, green, blue];
// console.log(colors);
let newColors = [];
// to check if the red, green or blue values are single digits
for (let element of colors) {
if (element.length !== 2) {
newColors.push((element = '0' + element));
} else {
newColors.push(element);
}
}
return newColors.join('');
};
const colorButton = document.getElementById('button');
colorButton.addEventListener('click', () => {
// const colorSwatches = document.getElementById('color-swatches');
// const hexVal = document.querySelector('hexvalue');
// const hexVal = document.getElementById('hexvalue');
// if(hexVal) {
// hexVal.textContent = '';
// }
// const colorSwatch = document.querySelector(`.${concatSwatch}`);
// colorSwatch.removeChild(hexVal);
// colorSwatches.removeChild(hexVal)
getColor();
});
/** COPY TO CLIPBOARD FUNCTION */
//copy button attributes
const copyButton = document.getElementById('copy-button');
copyButton.addEventListener('click', () => copyClip());
copyButton.addEventListener('mouseout', () => hoverMessage());
// changing the image button
copyButton.addEventListener('mouseover', () => {
const darkImage = document.getElementById('img-button');
darkImage.src = 'copy-black.png';
});
copyButton.addEventListener('mouseout', () => {
const darkImage = document.getElementById('img-button');
darkImage.src = 'copy.png';
});
function copyClip() {
const hexes = document.querySelectorAll(".hex1, .hex2, .hex3, .hex4, .hex5, .hex6");
const hexCodes = Array.from(hexes).map((elem) => elem.textContent).join("\n");
navigator.clipboard.writeText(hexCodes);
const buttonDiv = document.getElementById("message");
buttonDiv.innerHTML = "Copied!";
}
function hoverMessage() {
const messageDisplay = document.getElementById("message");
messageDisplay.innerHTML = "Copy to clipboard";
}