Repository navigation
Expand file tree
/
Copy pathRGB selector.html
More file actions
165 lines (158 loc) · 4.71 KB
/
Copy pathRGB selector.html
File metadata and controls
165 lines (158 loc) · 4.71 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
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<style>
.infobar{
position:relative;
display:block;
text-align:center;
margin-top:20px;
}
.innerBar{
display:inline;
}
.bar{
margin-left:20px;
position:relative;
display:inline-block;
width:255px;
height:5px;
background:rgb(192,192,192);
cursor:pointer;
}
.slider{
position:absolute;
height:20px;
width:10px;
top:-7px;
background:rgb(64,64,64);
cursor:pointer;
left:0;
}
#color{
display:block;
position:relative;
margin:20px auto;
width:250px;
height:250px;
background:black;
}
.num{width:4em;}
.Name,.input{
display:inline-block;
}
.Name{width:50px;}
.input{width:50px;}
</style>
</head>
<body id="body">
<div class="infobar"><div class="innerBar"><div class="Name">Red:</div><div class="input"><input max=255 min=0 class="num" id="r_in" type = "number"/></div><div class="bar" id="red"><div class="slider" id="r"></div></div></div></div>
<div class="infobar"><div class="innerBar"><div class="Name">Green:</div><div class="input"><input max=255 min=0 class="num" id="g_in" type = "number"/></div><div class="bar" id="green"><div class="slider" id="g"></div></div></div></div>
<div class="infobar"><div class="innerBar"><div class="Name">Blue:</div><div class="input"><input max=255 min=0 class="num" id="b_in" type = "number"/></div><div class="bar" id="blue"><div class="slider" id="b"></div></div></div></div>
<div id="color"></div>
<script>
var red = document.getElementById('r');
var green = document.getElementById('g');
var blue = document.getElementById('b');
var bars = document.getElementsByClassName("bar");
var mouseDown = false;
var interval;
var selected;
document.getElementById("r_in").addEventListener("input",inputChange);
document.getElementById("g_in").addEventListener("input",inputChange);
document.getElementById("b_in").addEventListener("input",inputChange);
document.getElementById("body").addEventListener("mouseup",undrag);
document.getElementById("body").addEventListener("mousemove",mouseMove);
for (var i = 0; i < bars.length; i++)
{
bars[i].addEventListener("click",click);
bars[i].addEventListener("mousedown",drag);
}
function inputChange(){
var value = this.value;
if (value < 0)
value = 0;
if (value > 255)
value = 255;
getPartner(this).slider.style.left = value + "px";
setBoxColor();
}
function getPartner(element)
{
var target,slider,input;
switch(element.id)
{
case "r":
case "red":
case "r_in":
target = document.getElementById("red");
slider = document.getElementById("r");
input = document.getElementById("r_in");
break;
case "g":
case "green":
case "g_in":
target = document.getElementById("green");
slider = document.getElementById("g");
input = document.getElementById("g_in");
break;
case "b":
case "blue":
case "b_in":
target = document.getElementById("blue");
slider = document.getElementById("b");
input = document.getElementById("b_in");
break;
}
return {bar:target, slider:slider};
}
function click(e){
var pair = getPartner(e.target);
var bar = pair.bar;
var slider = pair.slider;
var block = bar.getBoundingClientRect();
var x = e.pageX - block.left;
slider.style.left = x + "px";
setBoxColor();
}
function drag(e){
mouseDown = true;
selected = getPartner(e.target).slider;
}
function mouseMove(e){
if (mouseDown)
{ var slider = getPartner(selected).slider;
var x = e.pageX - getPartner(selected).bar.getBoundingClientRect().left;
if (x < 0)
x = 0;
if (x > 255)
x = 255;
slider.style.left = x + "px";
setBoxColor();
}
}
function undrag(){
mouseDown = false;
}
function reset(value){
if (value !== parseInt(value))
value = 0;
return value;
}
function setBoxColor(){
var redVal = parseInt(document.getElementById("r").style.left);
var greenVal = parseInt(document.getElementById("g").style.left);
var blueVal = parseInt(document.getElementById("b").style.left);
redVal = reset(redVal);
greenVal = reset(greenVal);
blueVal = reset(blueVal);
document.getElementById("r_in").value = redVal;
document.getElementById("g_in").value = greenVal;
document.getElementById("b_in").value = blueVal;
console.log("rgb(" + redVal + "," + greenVal + "," + blueVal + ")");
document.getElementById("color").style.background = "rgb(" + redVal + "," + greenVal + "," + blueVal + ")";
}
</script>
</body>
</html>