-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomColors.html
More file actions
176 lines (162 loc) · 4.84 KB
/
CustomColors.html
File metadata and controls
176 lines (162 loc) · 4.84 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Custom Colors</title>
<style type='text/css'>
canvas {
border: 1px solid black;
margin-top: 20px;
margin-left: 10px;
}
.snippet td, .snippet input {
font: 24pt Consolas, Courier New, monospace;
text-align: center;
}
.labels td, input.color {
font: 12pt Calibri, Arial, sans-serif;
text-align: center;
}
td.top {
vertical-align: top;
}
td span.k { color: rgb(0,0,240); }
.sliders input[type='range'] {
width: 80px;
}
</style>
<script type="text/javascript" src="jscolor/jscolor.js"></script>
</head>
<body>
<p>Change the numbers or sliders and see what happens!</p>
<table>
<tr class=labels>
<td>
<td>red
<td>
<td>green
<td>
<td>blue
<td>
<td>(or click ↓)
</tr>
<tr class=snippet>
<td>g.setColor(<span class=k>new</span> Color(
<td><input size=3 id='red' value='255'>
<td>,
<td><input size=3 id='green' value='255'>
<td>,
<td><input size=3 id='blue' value='0'>
<td>));
<td class="top">
<input id="colorpicker" tabindex="-1" value="ffff00" class="color {onImmediateChange:'fixRGB(this);'}">
</tr>
<tr class=sliders>
<td>
<td><input type="range" value="255" id="redSlide" min="0" max="255" step="1">
<td>
<td><input type="range" value="255" id="greenSlide" min="0" max="255" step="1">
<td>
<td><input type="range" value="255" id="blueSlide" min="0" max="255" step="1">
<td>
</tr>
</table>
<table>
<tr class=snippet>
<td>g.fillRect(150, 100, 150, 150);
</tr>
</table>
<canvas id='canvas' width='800' height='500'></canvas>
<script type='text/javascript'>
"use strict";
var ctx;
var r, g, b, color;
var slidingR = false;
var slidingG = false;
var slidingB = false;
function update() {
if ( slidingR ) {
document.getElementById('red').value = document.getElementById('redSlide').value;
}
else {
document.getElementById('redSlide').value = document.getElementById('red').value;
}
if ( slidingG ) {
document.getElementById('green').value = document.getElementById('greenSlide').value;
}
else {
document.getElementById('greenSlide').value = document.getElementById('green').value;
}
if ( slidingB ) {
document.getElementById('blue').value = document.getElementById('blueSlide').value;
}
else {
document.getElementById('blueSlide').value = document.getElementById('blue').value;
}
r = retrieveColor('red');
g = retrieveColor('green');
b = retrieveColor('blue');
color = "rgb(" + r + "," + g + "," + b + ")";
document.getElementById('colorpicker').color.fromRGB(r/255, g/255, b/255);
}
function retrieveColor( id ) {
var val = s2i( document.getElementById(id).value );
if ( val < 0 ) {
val = 0;
document.getElementById(id).value = "0";
}
else if ( val > 255 ) {
val = 255;
document.getElementById(id).value = "255";
}
return val;
}
function fixRGB(color) {
document.getElementById('red' ).value = Math.round(255 * color.rgb[0]);
document.getElementById('green').value = Math.round(255 * color.rgb[1]);
document.getElementById('blue' ).value = Math.round(255 * color.rgb[2]);
}
function s2i( s ) {
var n = 0;
if ( s.length > 0 ) {
var test = parseInt(s, 10);
if ( ! isNaN(test) )
n = test;
}
return n;
}
function draw(ctx) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// draw arc
ctx.fillStyle = color;
ctx.fillRect(150,100,150,150);
}
function draggR() { slidingR = true; }
function donedraggR() {
slidingR = false;
document.getElementById('red').value = document.getElementById('redSlide').value;
}
function draggG() { slidingG = true; }
function donedraggG() {
slidingG = false;
document.getElementById('green').value = document.getElementById('greenSlide').value;
}
function draggB() { slidingB = true; }
function donedraggB() {
slidingB = false;
document.getElementById('blue').value = document.getElementById('blueSlide').value;
}
function main() {
ctx = document.getElementById('canvas').getContext("2d");
document.getElementById('redSlide').addEventListener('input', draggR);
document.getElementById('redSlide').addEventListener('change', donedraggR);
document.getElementById('greenSlide').addEventListener('input', draggG);
document.getElementById('greenSlide').addEventListener('change', donedraggG);
document.getElementById('blueSlide').addEventListener('input', draggB);
document.getElementById('blueSlide').addEventListener('change', donedraggB);
setInterval(function() { update(); draw(ctx); }, 1000/60);
}
main();
</script>
</body>
</html>