summaryrefslogtreecommitdiff
path: root/public/js/color.js
blob: dff865c3ab782e960d807f017eeaf7ecc8acada6 (plain)
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
function Color (opt) {
  if (opt.length == 3) {
    this.r = opt[0];
    this.g = opt[1];
    this.b = opt[2];
    this.a = 1;
  }
  else if (opt.length == 4) {
    this.r = opt[0];
    this.g = opt[1];
    this.b = opt[2];
    this.a = opt[3];
  }
}
Color.prototype.toString = function(){
  return "rgba(" + this.r + "," + this.g + "," + this.b + "," + this.a + ")";
}
Color.prototype.rgb = function(){
  return [ this.r, this.g, this.b ];
}
Color.prototype.rgba = function(){
  return [ this.r, this.g, this.b, this.a ];
}
Color.prototype.hsl = function(){
}
Color.prototype.copy = function(){
  return new Color( this.rgba() );
}
Color.prototype.alpha = function(a){
  var c = this.copy()
  c.a = a;
  return c;
}
Color.prototype.swatch = function(){
  var that = this;
  var el = document.createElement("div");
  el.className = "swatch";
  el.style.backgroundColor = this.toString();
  document.getElementById("palette").appendChild(el);
  el.onclick = function(){
    color = that;
    makeBrushes();
    brush = brush.copy({ color: color });
  }
}


var colors = {
  red: new Color([ 255,0,0,0.1 ]),
  blue: new Color([ 0,0,255 ]),
  black: new Color([ 0,0,0 ]),
  green: new Color([ 0,128,0 ]),
  cyan: new Color([ 0,255,255 ]),
  yellow: new Color([ 255,255,0 ]),
  peru: new Color([ 205,133,63 ]),
}

for (var c in colors) {
  if (colors.hasOwnProperty(c)) {
    colors[c].swatch();
  }
}

var color = colors.black;