-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
257 lines (235 loc) · 9.66 KB
/
index.html
File metadata and controls
257 lines (235 loc) · 9.66 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
<html>
<head>
<title>Relative Color Sandbox</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<style>
.borderless, html, body {
margin: 0;
padding: 0;
border: 0;
}
body {
font-family: sans-serif;
background-color: #f4f4f4;
}
.hidden {
display: none;
}
@media screen and (max-width: 810px) {
body {
margin-left: 0;
margin-right: 0;
}
td {
width: 80px;
height: 80px;
}
}
@media screen and (min-width: 811px) {
body {
margin-top: 4em;
margin-left: 10em;
margin-right: 10em;
}
td {
width: 100px;
height: 100px;
}
}
</style>
<script src="https://cdn.jsdelivr.net/npm/baseline-status@1/baseline-status.min.js" type="module"></script>
<script type="module" src="./simple-color-picker.js"></script>
</head>
<body>
<header>
<h1>Relative Color Sandbox <baseline-status featureId="relative-color"></baseline-status></h1>
</header>
<main>
<div id="colorTableContainer">
</div>
<simple-color-picker class="hidden" id="colorEditor" value="#00ff00"></simple-color-picker>
</main>
<script>
// This sets up a table for visualizing relative color CSS.
function getCachedValue(name, defaultValue) {
let storedValueJson = localStorage.getItem(name);
let value = defaultValue;
if (storedValueJson) {
try {
value = JSON.parse(storedValueJson);
} catch (e) {
console.error(`Error parsing stored value for ${name}:`, e);
localStorage.removeItem(name);
}
}
return value;
}
let sourceColors = getCachedValue("sourceColors", ['rgb(0, 255, 0)', 'rgb(0, 128, 220)', 'rgb(100, 0, 100)', 'rgb(200, 0, 0)']);
let relativeColors = getCachedValue("relativeColors", ['hsl(from <color> h 20 l)', 'lch(from <color> calc(l + 20) c h)', 'lch(from <color> calc(l - 20) c h)', 'hsl(from <color> calc(h + 180) s l)']);
function cacheColors() {
localStorage.setItem("sourceColors", JSON.stringify(sourceColors));
localStorage.setItem("relativeColors", JSON.stringify(relativeColors));
}
function addRelativeColor(style) {
relativeColors.push(style);
dataToHtml();
}
function addSourceColor(color) {
sourceColors.push(color);
dataToHtml();
}
function getCellInfo(tableRowIdx, tableColIdx) {
const dataInfo = tableRowColToInfo(tableRowIdx, tableColIdx);
let text = "";
let backgroundColor = "rgba(255, 255, 255, 0)";
const sourceColor = sourceColors[dataInfo.sourceColorIdx];
const relativeColor = relativeColors[dataInfo.relativeColorIdx];
if (dataInfo.isRelativeColor) {
// Top row of relative color styles
text = relativeColor;
} else if (dataInfo.isAddRelativeColor) {
// Top right add button
text = "Add Relative Color";
backgroundColor = "lightgray";
} else if (dataInfo.isSourceColor) {
// Left column of source colors
text = sourceColor;
backgroundColor = sourceColor;
} else if (dataInfo.isResultColors) {
// Color cells in the middle
backgroundColor = relativeColor.replace("<color>", sourceColor);
} else if (dataInfo.isRemoveSourceColor) {
// Right column remove buttons
text = "Remove Color";
} else if (dataInfo.isRemoveRelativeColor) {
// Bottom row remove relative color buttons
text = "Remove Relative Color";
} else if (dataInfo.isAddSourceColor) {
// Bottom left add button
text = "Add Color";
backgroundColor = "lightgray";
}
return { text, backgroundColor };
}
let selectionContext = {
rowIdx: -1,
colIdx: -1,
clearEventHandler: null
}
// This function maps from table position row/column to info about what that cell represents.
// The layout of the table is as follows:
// The upper left corner is empty
// The upper right corner is an "Add Relative Color" button
// The upper row (except corners) are the relative colors
// The bottom left corner is an "Add Color" button
// The left column (except corners) are the source colors
// The bottom right corner is empty
// The right column (except corners) are "Remove Color" buttons
// The bottom row (except corners) are "Remove Relative Color" buttons
// All the rest of the inner cells are the result colors (the result of a source color run through a relative color)
function tableRowColToInfo(tableRowIdx, tableColIdx) {
let isSourceColor = tableColIdx == 0 && tableRowIdx > 0 && tableRowIdx <= sourceColors.length;
let isRemoveSourceColor = tableColIdx == relativeColors.length + 1 && tableRowIdx > 0 && tableRowIdx <= sourceColors.length;
let sourceColorIdx = tableRowIdx - 1;
let isRelativeColor = tableRowIdx == 0 && tableColIdx > 0 && tableColIdx <= relativeColors.length;
let isRemoveRelativeColor = tableRowIdx == sourceColors.length + 1 && tableColIdx > 0 && tableColIdx <= relativeColors.length;
let relativeColorIdx = tableColIdx - 1;
let isAddSourceColor = tableRowIdx == sourceColors.length + 1 && tableColIdx == 0;
let isAddRelativeColor = tableRowIdx == 0 && tableColIdx == relativeColors.length + 1;
let isResultColors = tableRowIdx > 0 && tableRowIdx <= sourceColors.length && tableColIdx > 0 && tableColIdx <= relativeColors.length;
return {
isSourceColor,
isRemoveSourceColor,
sourceColorIdx,
isRelativeColor,
isRemoveRelativeColor,
relativeColorIdx,
isAddSourceColor,
isAddRelativeColor,
isResultColors,
}
}
function cellSelected(tableRowIdx, tableColIdx) {
const selectionInfo = tableRowColToInfo(tableRowIdx, tableColIdx);
const cellChanged = (selectionContext.rowIdx != tableRowIdx ||
selectionContext.colIdx != tableColIdx);
if (selectionContext.clearEventHandler) {
selectionContext.clearEventHandler();
selectionContext.clearEventHandler = null;
}
selectionContext.rowIdx = tableRowIdx;
selectionContext.colIdx = tableColIdx;
if (selectionInfo.isRelativeColor) {
// Top row of relative color styles
const relativeColorIdx = selectionInfo.relativeColorIdx;
relativeColors[relativeColorIdx] = prompt("Edit the relative color style:", relativeColors[relativeColorIdx]) || relativeColors[relativeColorIdx];
dataToHtml();
} else if (selectionInfo.isAddRelativeColor) {
// Generate random relative style
const style = `hsl(from <color> calc(h + ${Math.floor(Math.random() * 360)}) 100 50)`;
addRelativeColor(style);
} else if (selectionInfo.isSourceColor) {
// Left column of source colors
// When clicking a source color, it selects it and opens the color picker
// Clicking the same source color again closes the color picker
const colorPicker = document.getElementById('colorEditor');
if (colorPicker.classList.contains('hidden')) {
colorPicker.classList.remove('hidden');
}
const sourceColorIdx = selectionInfo.sourceColorIdx;
colorPicker.value = sourceColors[sourceColorIdx];
const onColorChange = (event) => {
sourceColors[sourceColorIdx] = event.detail.value;
dataToHtml();
};
colorPicker.addEventListener('input', onColorChange);
selectionContext.clearEventHandler = () => {
colorPicker.removeEventListener('input', onColorChange);
};
} else if (selectionInfo.isAddSourceColor) {
// Add a random color and then open the color picker for it
const randomColor = `hsl(${Math.floor(Math.random() * 360)}, 100%, 50%)`;
addSourceColor(randomColor);
cellSelected(sourceColors.length, 0);
} else if (selectionInfo.isRemoveSourceColor) {
// Remove the source color
const sourceColorIdx = selectionInfo.sourceColorIdx;
sourceColors.splice(sourceColorIdx, 1);
dataToHtml();
} else if (selectionInfo.isRemoveRelativeColor) {
// Remove the relative color
const relativeColorIdx = selectionInfo.relativeColorIdx;
relativeColors.splice(relativeColorIdx, 1);
dataToHtml();
}
}
function dataToHtml() {
cacheColors();
if (sourceColors.length == 0 || relativeColors.length == 0) {
return;
}
const colorTableContainer = document.getElementById('colorTableContainer');
colorTableContainer.innerHTML = "";
const table = document.createElement('table');
colorTableContainer.appendChild(table);
for (var rowIdx = 0; rowIdx < sourceColors.length + 2; ++rowIdx) {
const tr = document.createElement('tr');
table.appendChild(tr);
for (var colIdx = 0; colIdx < relativeColors.length + 2; ++colIdx) {
const td = document.createElement('td');
tr.appendChild(td);
const { text, backgroundColor } = getCellInfo(rowIdx, colIdx);
td.textContent = text;
td.style.backgroundColor = backgroundColor;
td.addEventListener('click', ((rowIdx, colIdx) => {
cellSelected(rowIdx, colIdx);
}).bind(this, rowIdx, colIdx));
}
}
}
dataToHtml();
</script>
</body>
</html>