-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
253 lines (212 loc) · 4.91 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>
js has quirks
</title>
<style>
canvas{
padding: 0;
margin: auto;
display: block;
}
body{
text-align: center;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<br>
<span style="font-size:80px">background color:<span>
<br>
<input type="text" min="0" max="255" id="hex" style="width:400px; height:200px; font-size:80px; margin-right:50px;" value="0"></input>
<input type="number" min="0" max="255" id="red" style="width:200px; height:200px; font-size:80px" value="0"></input>
<input type="number" min="0" max="255" id="green" style="width:200px; height:200px; font-size:80px" value="0" ="0"></input>
<input type="number" min="0" max="255" id="blue" style="width:200px; height:200px; font-size:80px" value="0"></input>
<button style="width:400px; height:200px; font-size:80px;" onclick="alert('Please rightclick the image and press save image')">download</button>
<script>
let foreground;
let r = document.getElementById("red")
let g = document.getElementById("green")
let b = document.getElementById("blue")
let hex = document.getElementById("hex")
let canvas = document.getElementById("canvas")
hex.value = rgb2hex (r.value,g.value,b.value)
function setup(){
foreground = new Image();
foreground.src = "Quirk has quirks.png"
}
r.addEventListener("change",function(){
hex.value = rgb2hex (r.value,g.value,b.value)
if(r.value.toString() == ""){
r.value = 0;
}
if(g.value.toString() == ""){
g.value = 0;
}
if(b.value.toString() == ""){
b.value = 0;
}
if(r.value < 0){
r.value = 0;
}
if(g.value < 0){
g.value = 0;
}
if(b.value < 0){
b.value = 0;
}
if(r.value > 255){
r.value = 255;
}
if(g.value > 255){
g.value = 255;
}
if(b.value > 255){
b.value = 255;
}
})
g.addEventListener("change",function(){
hex.value = rgb2hex (r.value,g.value,b.value)
if(r.value.toString() == ""){
r.value = 0;
}
if(g.value.toString() == ""){
g.value = 0;
}
if(b.value.toString() == ""){
b.value = 0;
}
if(r.value < 0){
r.value = 0;
}
if(g.value < 0){
g.value = 0;
}
if(b.value < 0){
b.value = 0;
}
if(r.value > 255){
r.value = 255;
}
if(g.value > 255){
g.value = 255;
}
if(b.value > 255){
b.value = 255;
}
})
b.addEventListener("change",function(){
hex.value = rgb2hex (r.value,g.value,b.value)
if(r.value.toString() == ""){
r.value = 0;
}
if(g.value.toString() == ""){
g.value = 0;
}
if(b.value.toString() == ""){
b.value = 0;
}
if(r.value < 0){
r.value = 0;
}
if(g.value < 0){
g.value = 0;
}
if(b.value < 0){
b.value = 0;
}
if(r.value > 255){
r.value = 255;
}
if(g.value > 255){
g.value = 255;
}
if(b.value > 255){
b.value = 255;
}
})
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
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;
}
hex.addEventListener("change",function(){
if(hex.value == ""){
hex.value = rgb2hex (r.value,g.value,b.value)
}
let res = hexToRgb(hex.value)
console.log(res)
if(res == null){
alert("invalid hex code")
return
}
r.value = res.r
g.value = res.g
b.value = res.b
if(r.value.toString() == ""){
r.value = 0;
}
if(g.value.toString() == ""){
g.value = 0;
}
if(b.value.toString() == ""){
b.value = 0;
}
if(r.value < 0){
r.value = 0;
}
if(g.value < 0){
g.value = 0;
}
if(b.value < 0){
b.value = 0;
}
if(r.value > 255){
r.value = 255;
}
if(g.value > 255){
g.value = 255;
}
if(b.value > 255){
b.value = 255;
}
})
function hex2rgb(hex) {
// long version
r = hex.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
if (r) {
return r.slice(1,4).map(function(x) { return parseInt(x, 16); });
}
// short version
r = hex.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i);
if (r) {
return r.slice(1,4).map(function(x) { return 0x11 * parseInt(x, 16); });
}
return null;
}
function draw(){
canvas.width = foreground.width
canvas.height = foreground.height
background(r.value,g.value,b.value)
blit(0,0,canvas.width,canvas.height,foreground)
}
function rgb2hex(red, green, blue) {
var rgb = blue | (green << 8) | (red << 16);
return '#' + (0x1000000 + rgb).toString(16).slice(1)
}
document.body.style.zoom = "25%"
</script>
<script src="graphics.js" type="text/javascript"></script>
</body>
</html>