Repository navigation
Expand file tree
/
Copy pathColorPallet.js
More file actions
186 lines (154 loc) · 4.63 KB
/
Copy pathColorPallet.js
File metadata and controls
186 lines (154 loc) · 4.63 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
177
178
179
180
181
182
183
184
185
186
Dave_js.colorPallet = function(t, n) {
var self = this;
//make sure we have valid input
//valid types are:
// "rainbow", rev-rainbow" (reverse rainbow),
// "redblue", "rev-redblue",
// "blackwhite", "rev-blackwhite",
if (
t.indexOf("rainbow") == -1 &&
t.indexOf("redblue") == -1 &&
t.indexOf("blackwhite") == -1
) {
t = "rainbow";
}
//make sure the number of colors is between 1 and 255
if (isNaN(n) || n === "" || n <= 0) { n = 1;}
else if (n > 255) { n = 255; }
var type = t;
var numOfColors = n;
//define color object
self.color = function(r,g,b){
var self = this;
var vals = {};
self.toHexStr = function(){
var self = this;
function numToHex(num){
var hexRef =[
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E", "F"
];
var upper = num % 16;
var lower = parseInt(num / 16, 10);
return hexRef[upper] + "" + hexRef[lower];
}
return "#" + numToHex(r) + numToHex(g) + numToHex(b);
};
self.toRgbStr = function(){
return "rgb(" + r + "," + g + "," + b + ")";
};
};
//an array of color objects
var pallet = [];
//Acessors
self.setType = function(t){
type = t;
};
self.setNumOfColors = function(n){
numOfColors = n;
};
self.putPalletVal = function(r, g, b){
pallet.push(new self.color(r, g, b));
};
self.getType = function(){
return type;
};
self.getNumOfColors = function(){
return numOfColors;
};
self.getColors = function(){
return pallet;
};
self.reversePallet = function(){
pallet = pallet.reverse();
};
};
//calculates the pallet values
Dave_js.colorPallet.prototype.buildPallet = function(){
var self = this;
var num = self.getNumOfColors();
var color_i, stepSize, midpoint;
if (self.getType().indexOf("rainbow") != -1) {
stepSize = Math.floor(2 * (255 / num));
midpoint = Math.floor(num / 2);
//reduce the loop by two because we manually set the last color
num -= 1;
for (color_i = 0; color_i < midpoint; color_i++) {
self.putPalletVal(
(255 - (color_i * stepSize)), (color_i * stepSize), 0
);
}
for (color_i = midpoint; color_i < num; color_i++) {
self.putPalletVal(
0,
(255 - ((color_i - midpoint) * stepSize)),
((color_i - midpoint) * stepSize)
);
}
//get last color
self.putPalletVal(0, 0, 255);
} else if (self.getType().indexOf("redblue") != -1) {
stepSize = Math.floor(255 / num);
for (color_i = 0; color_i < num; color_i++) {
self.putPalletVal((255 - (color_i * stepSize)), 0, (color_i * stepSize));
}
} else if (self.getType().indexOf("blackwhite") != -1) {
stepSize = Math.floor(255 / num);
for (color_i = 0; color_i < num; color_i++) {
self.putPalletVal(
(color_i * stepSize), (color_i * stepSize), (color_i * stepSize)
);
}
}
if (self.getType().indexOf("rev") != -1) {
self.reversePallet();
}
};
//Takes a div element as input and fills it with color boxes
//The size input determines the pixel width of the color boxes
Dave_js.colorPallet.prototype.displayColors = function(
divEl, width, height, border
){
var self = this;
var pallet = self.getColors();
var num = self.getNumOfColors();
//check for a good size
if (width === 0 || isNaN(width)) {width = 5;}
if (height === 0 || isNaN(height)) {height = 5;}
//make a valid border size
if (isNaN(border)) {border = 0;}
//make sure a valid div element was given
if (divEl.tagName != "DIV") {return;}
try{
var tmpEl;
for (var color_i = 0; color_i < num; color_i++) {
tmpEl = document.createElement("div");
tmpEl.style.backgroundColor = pallet[color_i].toRgbStr();
tmpEl.style.border = "solid";
tmpEl.style.borderWidth = border + "px";
tmpEl.style.float = "left";
tmpEl.style.width = width + "px";
tmpEl.style.height = height + "px";
divEl.appendChild(tmpEl);
}
tmpEl = document.createElement("div");
tmpEl.style.float = "clear";
divEl.appendChild(tmpEl);
}catch(err){
}
};
//Return an array of color codes in rgb or hex format
Dave_js.colorPallet.prototype.getPallet = function(f){
var self = this;
var pallet = self.getColors();
var num = self.getNumOfColors();
var out = new Array(num);
for (var color_i = 0; color_i < num; color_i++) {
if (f == "hex") {
out[color_i] = pallet[color_i].toHexStr();
} else {
out[color_i] = pallet[color_i].toRgbStr();
}
}
return out;
};