forked from eric-wieser/snakes
/
color.js
100 lines (93 loc) · 3.16 KB
/
color.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
Color = function(r, g, b) {
this.r = r ? Color.clipComponent(r) : 0;
this.g = g ? Color.clipComponent(g) : 0;
this.b = b ? Color.clipComponent(b) : 0;
}
var rand = function(min, max) { return min + (max - min) * Math.random(); }
//Mutating methods
Color.prototype.randomize = function(range) {
this.r = rand(Color.clipComponent(this.r - range), Color.clipComponent(this.r + range));
this.g = rand(Color.clipComponent(this.g - range), Color.clipComponent(this.g + range));
this.b = rand(Color.clipComponent(this.b - range), Color.clipComponent(this.b + range));
return this;
};
Color.prototype.lerp = function(that, x) {
var y = 1 - x;
this.r = this.r*y + that.r*x;
this.g = this.g*y + that.g*x;
this.b = this.b*y + that.b*x;
return this;
};
Color.prototype.invert = function() {
this.r = 255-this.r;
this.g = 255-this.g;
this.b = 255-this.b;
return this;
}
Color.prototype.set = function(r, g, b) {
this.r = Color.clipComponent(r);
this.g = Color.clipComponent(g);
this.b = Color.clipComponent(b);
return this;
}
//Non-mutating versions
Color.prototype.inverted = function() { return this.clone().invert(); }
Color.prototype.lerped = function(that, x) { return this.clone().lerp(that, x); }
Color.prototype.randomized = function(x) { return this.clone().randomize(x); }
Color.prototype.toString = function() {
return 'rgb(' + Math.round(this.r) + ', ' + Math.round(this.g) + ', ' + Math.round(this.b) + ')';
};
Color.prototype.toHexString = function() {
return '#' + this.toInt().toString(16);
};
Color.prototype.toInt = function(rgb) {
return Math.round(this.r) << 16 | Math.round(this.g) << 8 | Math.round(this.b);
}
Color.fromInt = function(rgb) {
var b = rgb & 0xff;
var g = (rgb >>= 8) & 0xff;
var r = (rgb >>= 8) & 0xff;
return new Color(r, g, b);
}
Color.prototype.clone = function() {
var c = new Color;
c.r = this.r, c.b = this.b, c.g = this.g
return c;
}
Color.prototype.isIdenticalTo = function(color) {
return this.r == color.r && this.g == color.g && this.b == color.b;
}
Color.random = function(r, g, b, range) {
return new Color(r || 128, g || 128, b || 128).randomize(range || 128);
}
Color.clipComponent = function(x) {
return x > 255 ? 255 : x < 0 ? 0 : x;
}
Color.ify = function(data) {
if(typeof data == "number")
return Color.fromInt(data);
else if(data instanceof Object)
return new Color(data.r, data.g, data.b);
}
Color.randomHue = function() {
var a = Math.random() * 3;
var b = Math.random() * 256;
if(a < 1)
return new Color(255, b, 256 - b);
else if(a < 2)
return new Color(256 - b, 255, b);
else
return new Color(b, 256 - b, 255);
}
//Primaries
Color.red = function() { return new Color(255, 0, 0); }
Color.green = function() { return new Color(0, 255, 0); }
Color.blue = function() { return new Color(0, 0, 255); }
//secondaries
Color.yellow = function() { return new Color(255, 255, 0); }
Color.cyan = function() { return new Color(0, 255, 255); }
Color.magenta = function() { return new Color(255, 0, 255); }
//grayscale
Color.white = function() { return new Color(255, 255, 255); }
Color.gray = function() { return new Color(128, 128, 128); }
Color.black = function() { return new Color( 0, 0, 0); }