2 Raphael.colorwheel = function (x, y, size, initcolor, element) {
3 return new ColorWheel(x, y, size, initcolor, element);
7 return (x < 0) * 180 + Math.atan(-y / -x) * 180 / pi;
9 var doc = document, win = window;
10 var addEvent = (function () {
11 if (doc.addEventListener) {
12 return function (obj, type, fn, element) {
13 var f = function (e) {
14 return fn.call(element, e);
16 obj.addEventListener(type, f, false);
18 obj.removeEventListener(type, f, false);
22 } else if (doc.attachEvent) {
23 return function (obj, type, fn, element) {
24 var f = function (e) {
25 return fn.call(element, e || win.event);
27 obj.attachEvent("on" + type, f);
28 var detacher = function () {
29 obj.detachEvent("on" + type, f);
36 var ColorWheel = function (x, y, size, initcolor, element) {
38 var w3 = 3 * size / 200,
41 segments = pi * size / 5,
44 padding = 2 * size / 200,
47 var H = 1, S = 1, B = 1, s = size - (size20 * 4);
48 var r = element ? Raphael(element, size, size) : Raphael(x, y, size, size),
49 xy = s / 6 + size20 * 2 + padding,
50 wh = s * 2 / 3 - padding * 2;
56 var a = pi / 2 - pi * 2 / segments * 1.3,
58 R2 = size2 - padding - size20 * 2,
59 path = ["M", size2, padding, "A", R, R, 0, 0, 1, R * Math.cos(a) + R + padding, R - R * Math.sin(a) + padding, "L", R2 * Math.cos(a) + R + padding, R - R2 * Math.sin(a) + padding, "A", R2, R2, 0, 0, 0, size2, padding + size20 * 2, "z"].join();
60 for (var i = 0; i < segments; i++) {
63 fill: "hsb(" + i * (255 / segments) + ", 255, 200)",
64 rotation: [(360 / segments) * i, size2, size2]
67 r.path(["M", size2, padding, "A", R, R, 0, 1, 1, size2 - 1, padding, "l1,0", "M", size2, padding + size20 * 2, "A", R2, R2, 0, 1, 1, size2 - 1, padding + size20 * 2, "l1,0"]).attr({
71 t.cursorhsb = r.set();
72 var h = size20 * 2 + 2;
73 t.cursorhsb.push(r.rect(size2 - h / fi / 2, padding - 1, h / fi, h, 3 * size / 200).attr({
78 t.cursorhsb.push(t.cursorhsb[0].clone().attr({
83 t.ring = r.path(["M", size2, padding, "A", R, R, 0, 1, 1, size2 - 1, padding, "l1,0M", size2, padding + size20 * 2, "A", R2, R2, 0, 1, 1, size2 - 1, padding + size20 * 2, "l1,0"]).attr({
90 t.main = r.rect(xy, xy, wh, wh).attr({
100 t.square = r.rect(xy - 1, xy - 1, wh + 2, wh + 2).attr({
104 fill: "90-#000-#000",
109 t.cursor.push(r.circle(size2, size2, size20 / 2).attr({
114 t.cursor.push(t.cursor[0].clone().attr({
128 t.hsbon = addEvent(t.ring.node, "mousedown", function (e) {
129 this.hsbOnTheMove = true;
130 this.setH(e.clientX - this.x - this.size2, e.clientY - this.y - this.size2);
131 this.docmove = addEvent(doc, "mousemove", this.docOnMove, this);
132 this.docup = addEvent(doc, "mouseup", this.docOnUp, this);
134 t.clron = addEvent(t.square.node, "mousedown", function (e) {
135 this.clrOnTheMove = true;
136 this.setSB(e.clientX - this.x, e.clientY - this.y);
137 this.docmove = addEvent(doc, "mousemove", this.docOnMove, this);
138 this.docup = addEvent(doc, "mouseup", this.docOnUp, this);
140 t.winunload = addEvent(win, "unload", function () {
143 this.docmove && this.docmove();
144 this.docup && this.docup();
148 t.color(initcolor || "#f00");
149 this.onchanged && this.onchanged(this.color());
151 ColorWheel.prototype.setH = function (x, y) {
154 this.cursorhsb.rotate(d + 90, this.size2, this.size2);
155 this.H = (d + 90) / 360;
156 this.main.attr({fill: "hsb(" + this.H + ",1,1)"});
157 this.onchange && this.onchange(this.color());
159 ColorWheel.prototype.setSB = function (x, y) {
160 x < this.size2 - this.wh / 2 && (x = this.size2 - this.wh / 2);
161 x > this.size2 + this.wh / 2 && (x = this.size2 + this.wh / 2);
162 y < this.size2 - this.wh / 2 && (y = this.size2 - this.wh / 2);
163 y > this.size2 + this.wh / 2 && (y = this.size2 + this.wh / 2);
164 this.cursor.attr({cx: x, cy: y});
165 this.B = 1 - (y - this.xy) / this.wh;
166 this.S = (x - this.xy) / this.wh;
167 this.onchange && this.onchange(this.color());
169 ColorWheel.prototype.docOnMove = function (e) {
170 if (this.hsbOnTheMove) {
171 this.setH(e.clientX - this.x - this.size2, e.clientY - this.y - this.size2);
173 if (this.clrOnTheMove) {
174 this.setSB(e.clientX - this.x, e.clientY - this.y);
176 e.preventDefault && e.preventDefault();
177 e.returnValue = false;
180 ColorWheel.prototype.docOnUp = function (e) {
181 this.hsbOnTheMove = this.clrOnTheMove = false;
186 this.onchanged && this.onchanged(this.color());
188 ColorWheel.prototype.remove = function () {
189 this.raphael.remove();
190 this.color = function () {
194 ColorWheel.prototype.color = function (color) {
196 color = Raphael.getRGB(color);
197 color = Raphael.rgb2hsb(color.r, color.g, color.b);
198 var d = color.h * 360;
202 this.cursorhsb.rotate(d, this.size2, this.size2);
203 this.main.attr({fill: "hsb(" + this.H + ",1,1)"});
204 var x = this.S * this.wh + this.xy,
205 y = (1 - this.B) * this.wh + this.xy;
206 this.cursor.attr({cx: x, cy: y});
209 return Raphael.hsb2rgb(this.H, this.S, this.B).hex;