-
Notifications
You must be signed in to change notification settings - Fork 0
/
color-scale2d.js
197 lines (177 loc) · 5.93 KB
/
color-scale2d.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
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
187
188
189
190
191
192
193
194
195
196
$(document).ready(function() {
var c1 = '#21313e';
var c2 = '#ec7063';
var c3 = '#73c332';
var c4 = '#2dedbb';
function check_lightness() {
// check if the lightness of the corner colors requires a 'dark' menu
if (chroma(c1).luminance() > 0.7) {
$('#test .row:first-child .cell:first-child .cell_menu').addClass('dark');
}
else {
$('#test .row:first-child .cell:first-child .cell_menu').removeClass('dark');
}
if (chroma(c2).luminance() > 0.7) {
$('#test .row:first-child .cell:last-child .cell_menu').addClass('dark');
}
else {
$('#test .row:first-child .cell:last-child .cell_menu').removeClass('dark');
}
if (chroma(c3).luminance() > 0.7) {
$('#test .row:last-child .cell:first-child .cell_menu').addClass('dark');
}
else {
$('#test .row:last-child .cell:first-child .cell_menu').removeClass('dark');
}
if (chroma(c4).luminance() > 0.7) {
$('#test .row:last-child .cell:last-child .cell_menu').addClass('dark');
}
else {
$('#test .row:last-child .cell:last-child .cell_menu').removeClass('dark');
}
}
function update_scale() {
var num_cells = $('#test .row:first-child .cell').length;
var num_rows = $('#test .row').length;
var lef_col_scale = chroma.scale([c1, c3]);
var rig_col_scale = chroma.scale([c2, c4]);
var grid = [];
var $jq, cl, cr, scale;
for (var r=0;r<num_rows;r++) {
cl = lef_col_scale(r/num_rows).hex();
cr = rig_col_scale(r/num_rows).hex();
scale = chroma.scale([cl,cr]);
for (var c=0;c<num_cells;c++) {
$jq = $('#test .row:nth-child('+ (r+1).toString() + ') .cell:nth-child('+(c+1).toString()+')');
$jq.css("background", scale(c / num_cells).hex());
$jq.children('.cell_code').html(scale(c / num_cells).hex());
}
}
check_lightness();
// adjust widths
$('#test .cell').css('width',(1/num_cells*100).toString() + '%');
}
function get_random_color() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function init() {
// creates a color picker dialog for top left cell
$('#test .row:first .cell:first .pick_color').colpick({
color:c1,
submit: 0,
colorScheme:'dark',
onChange:function(hsb,hex,rgb,el,bySetColor) {
c1 = '#'+ hex;
update_scale();
}
});
// creates a color picker dialog for top right cell
$('#test .row:first .cell:last .pick_color').colpick({
color:c2,
submit: 0,
colorScheme:'dark',
onChange:function(hsb,hex,rgb,el,bySetColor) {
c2 = '#'+ hex;
update_scale();
}
});
// creates a color picker dialog for bottom left cell
$('#test .row:last .cell:first .pick_color').colpick({
color:c3,
submit: 0,
colorScheme:'dark',
onChange:function(hsb,hex,rgb,el,bySetColor) {
c3 = '#'+ hex;
update_scale();
}
});
// creates a color picker dialog for bottom right cell
$('#test .row:last .cell:last .pick_color').colpick({
color:c4,
submit: 0,
colorScheme:'dark',
onChange:function(hsb,hex,rgb,el,bySetColor) {
c4 = '#'+ hex;
update_scale();
}
});
update_scale();
}
init();
/* =====================================
USER INTERACTIONS
===================================== */
// add a cell
$('#add_cell').click(function() {
var cell_html = '<div class="cell">';
cell_html += '<div class="cell_code"></div>';
cell_html += '</div>';
var num_rows = $('#test .row').length;
for (var i = 0; i < num_rows; i++) {
$('#test .row:nth-child('+(i+1).toString()+') .cell:last').before(cell_html);
};
$('#display_num_cells').html($('#test .row:first .cell').length);
update_scale();
});
// remove a cell
$('#sub_cell').click(function() {
if ($('#test .cell').length > 3) {
$('#test .cell:nth-last-child(2)').remove();
update_scale();
$('#display_num_cells').html($('#test .row:first .cell').length);
}
});
// add a row
$('#add_row').click(function() {
var num_cells = $('#test .row:first-child .cell').length;
var row_html = '<div class="row">';
for (var i = 0; i < num_cells; i++) {
row_html += '<div class="cell">';
row_html += '<div class="cell_code"></div>';
row_html += '</div>';
};
row_html += '</div>';
$('#test .row:last').before(row_html);
$('#display_num_rows').html($('#test .row').length);
update_scale();
});
// remove a row
$('#sub_row').click(function() {
if ($('#test .row').length > 3) {
$('#test .row:nth-last-child(2)').remove();
update_scale();
}
$('#display_num_rows').html($('#test .row').length);
});
// 'save' a scale
$('#save_scale').click(function() {
var $scale = $('#test').clone().removeAttr('id');
$scale.children('.row').first().children('.cell').last().children('.cell_menu').remove();
$scale.children('.row').first().children('.cell').first().children('.cell_menu').remove();
$scale.children('.row').last().children('.cell').last().children('.cell_menu').remove();
$scale.children('.row').last().children('.cell').first().children('.cell_menu').remove();
$('#saved_scales').append($scale);
});
// randomize colors
$('#test .row:first .cell:first .randomize').click(function() {
c1 = get_random_color();
update_scale();
});
$('#test .row:first .cell:last .randomize').click(function() {
c2 = get_random_color();
update_scale();
});
$('#test .row:last .cell:first .randomize').click(function() {
c3 = get_random_color();
update_scale();
});
$('#test .row:last .cell:last .randomize').click(function() {
c4 = get_random_color();
update_scale();
});
});