summaryrefslogtreecommitdiff
path: root/public/assets/js/util/color.js
blob: c76dbdf9d14eb5c30f4d1d68f4e54fda1f8ba1ba (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
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
function Color (r,g,b,a) {
  this.r = r
  this.g = g
  this.b = b
  this.a = a || 1.0
}
Color.prototype.toString = function(){
  return "rgba(" + Math.round(this.r) + "," + Math.round(this.g) + "," + Math.round(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.clone = function(){
  return new Color(this.r, this.g, this.b, this.a);
}
Color.prototype.alpha = function(a){
  var c = this.copy()
  c.a = a;
  return c;
}
Color.prototype.add = function(n){
  if (! n) return
  this.r = clamp(Math.round(this.r + n), 0, 255)
  this.g = clamp(Math.round(this.g + n), 0, 255)
  this.b = clamp(Math.round(this.b + n), 0, 255)
  return this
}
Color.prototype.mottle = function(n){
  n = n || 30
  this.r = clamp(this.r + randrange(-n, n), 0, 255)
  this.g = clamp(this.g + randrange(-n, n), 0, 255)
  this.b = clamp(this.b + randrange(-n, n), 0, 255)
  return this
}
Color.prototype.mottleRGB = function(r,g,b){
  r = r || 0 ; g = g || 0 ; b = b || 0
  this.r = clamp(this.r + randrange(-r, r), 0, 255)
  this.g = clamp(this.g + randrange(-g, g), 0, 255)
  this.b = clamp(this.b + randrange(-b, b), 0, 255)
  return this
}
Color.prototype.swatch = function(){
  var el = document.createElement("div");
  el.style.className = "swatch";
  el.style.width = 16 + "px";
  el.style.height = 16 + "px";
  el.style.backgroundColor = this.toString();
  $(el).data("color", this);
  return el;
}

var COLORS = {
  plain: new Color(230,240,240),
  ivory: new Color(240,240,235),
  pink: new Color(240,223,235),
  red: new Color(240,224,223),
  orange: new Color(240,232,223),
  yellow: new Color(240,240,231),
  green: new Color(233,240,231),
  blue: new Color(224,226,240),
  purple: new Color(235,231,240),
  black: new Color(32,32,37),
}

var DARK_COLORS = {
  plain: new Color(24,10,10),
  ivory: new Color(18,18,18),
  pink: new Color(40,23,35),
  red: new Color(40,24,23),
  orange: new Color(40,32,23),
  yellow: new Color(40,40,31),
  green: new Color(33,40,31),
  blue: new Color(24,26,40),
  purple: new Color(35,31,40),
  black: new Color(0,0,0),
}

function nighttime_quotient() {
  var q = -10;
  var date = new Date()
  var x;
  var h = date.getHours()
  var m = date.getMinutes()
  if (h < 5 || h > 23)
    { return q; }
  if (h >= 5 && h <= 7)
    { x = 60*60*3 - 60 * (h - 5) + m; }
  if (h >= 21 && h <= 23)
    { x = 60 * (h - 21) + m; }
  x /= 60*60*3;
  return q * x;
}
function get_color_from_time(){
  var date = new Date()
  var h = date.getHours()
  var m = date.getMinutes()
  var c;
  if ((h == 4 || h == 16) && m == 20)
    { c = "green"; }
  else if (h < 5)
    { c = "blue"; }
  else if (h >= 5 && h < 6)
    { c = "red"; }
  else if (h >= 6 && h < 9)
    { c = "orange"; }
  else if (h >= 9 && h < 12)
    { c = "yellow"; }
  else if (h >= 12 && h < 18)
    { c = "plain"; }
  else if (h >= 18 && h < 21)
    { c = "blue"; }
  else if (h >= 21)
    { c = "blue"; }
  else
    { c = "plain"; }
  return c;
}
function set_background_color_from_time(){
  var color_name = get_color_from_time()
  set_background_color(color_name)
}
function set_background_color(color_name){
  color_name = color_name || "plain"
  var color_set = window.matchMedia('prefers-color-scheme: dark').matches ? DARK_COLORS : COLORS
  var color = color_set[color_name]
    .clone()
    .mottleRGB(4,4,8)
//     .add(nighttime_quotient())
  document.body.style.backgroundColor = color.toString()
  $(document.body).toggleClass("black", color_name === "black")
}