-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgbColorSelector.js
90 lines (88 loc) · 3.16 KB
/
rgbColorSelector.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
function rgbColorSelector( coorX, coorY,alpha) {
function newRadian () {
radian = ((Math.PI / 2) - radian);
radian = (Math.PI / 6) - radian;
arcLength = radian * radius;
leftArcLength = itemArcLength - arcLength;
}
const allRadius = 127.5; //最大圆半径
var x = coorX;
var y = coorY;
//console.log("x:"+x+" y:"+y);
var allArcLength = ((Math.PI * allRadius) / 3); /*每一小段弧长*/
var radius = Math.sqrt(Math.pow((x - allRadius), 2) + Math.pow(y - allRadius, 2));
//console.log("所在圆半径:"+radius);
//三条线
//二
var Line2 = ((allRadius - x) * Math.sqrt(3) / 3) + allRadius;
//三
var Line3 = ((x - allRadius) * Math.sqrt(3) / 3) + allRadius;
var radian = Math.atan2(Math.abs(x - allRadius), Math.abs(allRadius - y)); //弧度
var arcLength = radian * radius; //弧长
var itemArcLength = (Math.PI * radius) / 3;//单项弧长
var leftArcLength = itemArcLength - arcLength; //剩余弧长
//console.debug("弧度:"+radian+"弧长:"+arcLength+"单项弧长:"+itemArcLength+"剩余弧长:"+leftArcLength);
if(x >= allRadius) {
//123
if(y <= Line2) {
//1
//console.debug("弧度:"+radian+"弧长:"+arcLength+"单项弧长:"+itemArcLength+"剩余弧长:"+leftArcLength);
colorR = allRadius + radius;
colorG = (arcLength * (allRadius + radius)) / itemArcLength;
colorB = allRadius - radius;
} else if(y > Line2 && y <= Line3) {
//2.5
//console.debug("弧度:"+radian+" 弧长:"+arcLength+" 单项弧长:"+itemArcLength+" 剩余弧长:"+leftArcLength);
newRadian();
if(y > allRadius) {
colorR = (arcLength * (allRadius + radius)) / itemArcLength;
} else {
colorR = (leftArcLength * (allRadius + radius)) / itemArcLength;
}
colorG = allRadius + radius;
colorB = allRadius - radius;
} else {
colorR = allRadius - radius;
colorG = allRadius + radius;
colorB = (leftArcLength * (allRadius + radius)) / itemArcLength;
}
} else {
//456
if(y >= Line2) {
colorR = allRadius - radius;
colorG = (leftArcLength * (allRadius + radius)) / itemArcLength;
colorB = allRadius + radius;
} else if(y < Line2 && y >= Line3) {
//console.debug("弧度:"+radian+" 弧长:"+arcLength+" 单项弧长:"+itemArcLength+" 剩余弧长:"+leftArcLength);
newRadian();
if(y > allRadius) {
colorR = (arcLength * (allRadius + radius)) / itemArcLength;
} else {
colorR = (leftArcLength * (allRadius + radius)) / itemArcLength;
}
colorG = allRadius - radius;
colorB = allRadius + radius;
} else {
colorR = allRadius + radius;
colorG = allRadius - radius;
colorB = (arcLength * (allRadius + radius)) / itemArcLength;
}
}
var stringbuffer=new StringBuffer();
stringbuffer.append("rgb(");
stringbuffer.append(parseInt(colorR));
stringbuffer.append(',');
stringbuffer.append(parseInt(colorG));
stringbuffer.append(',');
stringbuffer.append(parseInt(colorB));
var res = stringbuffer.toString();
// console.log(res);
var colorArr=new Array();
colorArr['r']=parseInt(colorR);
colorArr['g']=parseInt(colorG);
colorArr['b']=parseInt(colorB);
colorArr['a']=parseFloat(alpha);
colorArr['rgb']=res+")";
colorArr['rgba']=res+","+parseFloat(alpha)+")";
return colorArr;
}