-
Notifications
You must be signed in to change notification settings - Fork 37
/
colorpicker.js
199 lines (189 loc) · 7.15 KB
/
colorpicker.js
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
/*!
* Color Picker 0.1.0 - Raphael plugin
*
* Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
* Based on Color Wheel (http://jweir.github.com/colorwheel) by John Weir (http://famedriver.com)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
(function (Raphael) {
Raphael.colorpicker = function (x, y, size, initcolor, element) {
return new ColorPicker(x, y, size, initcolor, element);
};
Raphael.fn.colorPickerIcon = function (x, y, r) {
var segments = pi * r * 2 / Math.min(r / 8, 4);
var a = pi / 2 - pi * 2 / segments * 1.5,
path = ["M", x, y - r, "A", r, r, 0, 0, 1, r * Math.cos(a) + x, y - r * Math.sin(a), "L", x, y, "z"].join();
for (var i = 0; i < segments; i++) {
this.path(path).attr({
stroke: "none",
fill: "hsb(" + (segments - i) * (255 / segments) / 255 + ", 1, 1)",
transform: "r" + [90 + (360 / segments) * i, x, y]
});
}
return this.circle(x, y, r).attr({
fill: "r#fff-#fff",
"fill-opacity": 0,
"stroke-width": Math.round(r * .03),
stroke: "#fff"
});
};
var pi = Math.PI;
function angle(x, y) {
return (x < 0) * 180 + Math.atan(-y / -x) * 180 / pi;
}
var doc = document, win = window,
ColorPicker = function (x, y, size, initcolor, element) {
size = size || 200;
var w3 = 3 * size / 200,
w1 = size / 200,
fi = 1.6180339887,
size20 = size / 20,
size2 = size / 2,
padding = 2 * size / 200,
height = size + size20 * 2 + padding * 3,
t = this,
H = 1, S = 1, B = 1, s = size - (size20 * 4),
r = element ? Raphael(element, size, height) : Raphael(x, y, size, height),
xy = s / 6 + size20 * 2 + padding,
wh = s * 2 / 3 - padding * 2;
w1 < 1 && (w1 = 1);
w3 < 1 && (w3 = 1);
r.colorPickerIcon(size2, size2, size2 - padding);
t.cursor = r.set();
t.cursor.push(r.circle(size2, size2, size20 / 2).attr({
stroke: "#000",
opacity: .5,
"stroke-width": w3
}));
t.cursor.push(t.cursor[0].clone().attr({
stroke: "#fff",
opacity: 1,
"stroke-width": w1
}));
t.disc = r.circle(size2, size2, size2 - padding).attr({
fill: "#000",
"fill-opacity": 0,
stroke: "none",
cursor: "crosshair"
});
var style = t.disc.node.style;
style.unselectable = "on";
style.MozUserSelect = "none";
style.WebkitUserSelect= "none";
// brightness drawing
var h = size20 * 2 + 2;
t.brect = r.rect(padding + h / fi / 2, size + padding * 2, size - padding * 2 - h / fi, h - padding * 2).attr({
stroke: "#fff",
fill: "180-#fff-#000"
});
t.cursorb = r.set();
t.cursorb.push(r.rect(size - padding - h / fi, size + padding, ~~(h / fi), h, w3).attr({
stroke: "#000",
opacity: .5,
"stroke-width": w3
}));
t.cursorb.push(t.cursorb[0].clone().attr({
stroke: "#fff",
opacity: 1,
"stroke-width": w1
}));
t.btop = t.brect.clone().attr({
stroke: "#000",
fill: "#000",
opacity: 0
});
style = t.btop.node.style;
style.unselectable = "on";
style.MozUserSelect = "none";
style.WebkitUserSelect= "none";
t.bwidth = ~~(h / fi) / 2;
t.minx = padding + t.bwidth;
t.maxx = size - h / fi - padding + t.bwidth;
t.H = t.S = t.B = 1;
t.padding = padding;
t.raphael = r;
t.size2 = size2;
t.size20 = size20;
t.x = x;
t.y = y;
// events
t.disc.drag(function (dx, dy, x, y) {
t.docOnMove(dx, dy, x, y);
}, function (x, y) {
t.hsOnTheMove = true;
t.setHS(x - t.x, y - t.y);
}, function () {
t.hsOnTheMove = false;
});
t.btop.drag(function (dx, dy, x, y) {
t.docOnMove(dx, dy, x, y);
}, function (x, y) {
t.bOnTheMove = true;
t.setB(x - t.x);
}, function () {
t.bOnTheMove = false;
});
t.color(initcolor || "#fff");
this.onchanged && this.onchanged(this.color());
};
ColorPicker.prototype.setB = function (x) {
x < this.minx && (x = this.minx);
x > this.maxx && (x = this.maxx);
this.cursorb.attr({x: x - this.bwidth});
this.B = (x - this.minx) / (this.maxx - this.minx);
this.onchange && this.onchange(this.color());
};
ColorPicker.prototype.setHS = function (x, y) {
var X = x - this.size2,
Y = y - this.size2,
R = this.size2 - this.size20 / 2 - this.padding,
d = angle(X, Y),
rd = d * pi / 180;
isNaN(d) && (d = 0);
if (X * X + Y * Y > R * R) {
x = R * Math.cos(rd) + this.size2;
y = R * Math.sin(rd) + this.size2;
}
this.cursor.attr({cx: x, cy: y});
this.H = (1 - d / 360) % 1;
this.S = Math.min((X * X + Y * Y) / R / R, 1);
this.brect.attr({fill: "180-hsb(" + [this.H, this.S] + ",1)-#000"});
this.onchange && this.onchange(this.color());
};
ColorPicker.prototype.docOnMove = function (dx, dy, x, y) {
if (this.hsOnTheMove) {
this.setHS(x - this.x, y - this.y);
}
if (this.bOnTheMove) {
this.setB(x - this.x);
}
};
ColorPicker.prototype.remove = function () {
this.raphael.remove();
this.color = function () {
return false;
};
};
ColorPicker.prototype.color = function (color) {
if (color) {
color = Raphael.getRGB(color);
var hex = color.hex;
color = Raphael.rgb2hsb(color.r, color.g, color.b);
d = color.h * 360;
this.H = color.h;
this.S = color.s;
this.B = color.b;
this.cursorb.attr({x: this.B * (this.maxx - this.minx) + this.minx - this.bwidth});
this.brect.attr({fill: "180-hsb(" + [this.H, this.S] + ",1)-#000"});
var d = (1 - this.H) * 360,
rd = d * pi / 180,
R = (this.size2 - this.size20 / 2 - this.padding) * this.S,
x = Math.cos(rd) * R + this.size2,
y = Math.sin(rd) * R + this.size2;
this.cursor.attr({cx: x, cy: y});
return this;
} else {
return Raphael.hsb2rgb(this.H, this.S, this.B).hex;
}
};
})(window.Raphael);