aboutsummaryrefslogtreecommitdiff
path: root/color-picker.h
blob: d442c79eccabebcb7cf2005f0958819d8ccd05b0 (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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// -*- mode: c++ -*- 
#pragma once
 
/*
  this class implements a color theme editor as a LED mode
 
  when this mode is active, nothing gets sent to the host
 
  the Fn keys switch into editing mode, with that half housing the controls
 
  the controls are:
 
  * 8 keys for the 8 colors we currently use
  * 3 rows of 4 keys to change the active color in the HSV space
 
  pressing a key on the non-control half of the keyboard assigns the
  current color to that key
 */
 
#include <kaleidoscope/plugin/LEDControl.h>
#include <kaleidoscope/plugin/LEDControl/LEDUtils.h>
#include <kaleidoscope/plugin/FocusSerial.h>
 
class ColorPicker : public kaleidoscope::plugin::LEDMode {
public:
  ColorPicker(void) : current_index(0), which_half(BOTH_HALVES),
                      colors{}, map{},
                      hsv_colors{
                                 { .h=0.s=150.v=128 },
                                 { .h=32.s=150.v=128 },
                                 { .h=64.s=150.v=128 },
                                 { .h=96.s=150.v=128 },
                                 { .h=128.s=150.v=128 },
                                 { .h=160.s=150.v=128 },
                                 { .h=192.s=150.v=128 },
                                 { .h=224.s=150.v=128 },
                      }
  {
    for (uint8_t i=0;i<16;++i) {
      hsv& col = hsv_colors[i];
      colors[i] = hsvToRgb(col.h,col.s,col.v);
    }
  }
 
  kaleidoscope::EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
    if (!Kaleidoscope.has_leds || !is_active())
      return kaleidoscope::EventHandlerResult::OK;
 
    // only care at press time 
    if (!keyToggledOn(key_state))
      return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
 
    // uh? 
    if (row >= ROWS || col >= COLS)
      return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
 
    // pressing a Fn makes that half the "editing" half; pressing it 
    // again disables editing 
 
    picker_key key = whichKey(row,col);
    switch (key) {
    case OFF:
      break;
    case SHOW:
      map[row][col] = current_index; break;
    case SWITCH_LEFT:
      which_half = which_half == RIGHT_HALF ? BOTH_HALVES : RIGHT_HALF; break;
    case SWITCH_RIGHT:
      which_half = which_half == LEFT_HALF ? BOTH_HALVES : LEFT_HALF; break;
    case COLOR_1 ... COLOR_8:
      current_index = key - COLOR_1; break;
    case HUE_M10:
      updateCurrentColor(-10,0,0); break;
    case HUE_M1:
      updateCurrentColor(-1,0,0); break;
    case HUE_P1:
      updateCurrentColor(+1,0,0); break;
    case HUE_P10:
      updateCurrentColor(+10,0,0); break;
    case SAT_M10:
      updateCurrentColor(0,-10,0); break;
    case SAT_M1:
      updateCurrentColor(0,-1,0); break;
    case SAT_P1:
      updateCurrentColor(0,+1,0); break;
    case SAT_P10:
      updateCurrentColor(0,+10,0); break;
    case VAL_M10:
      updateCurrentColor(0,0,-10); break;
    case VAL_M1:
      updateCurrentColor(0,0,-1); break;
    case VAL_P1:
      updateCurrentColor(0,0,+1); break;
    case VAL_P10:
      updateCurrentColor(0,0,+10); break;
    };
 
    return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
  }
 
  kaleidoscope::EventHandlerResult onFocusEvent(const char *command) {
    if (Focus.handleHelp(command, PSTR("color-picker.dump")))
      return kaleidoscope::EventHandlerResult::OK;
 
    if (strncmp_P(command, PSTR("color-picker."), 13) != 0)
      return kaleidoscope::EventHandlerResult::OK;
 
    if (strcmp_P(command + 13, PSTR("dump")) != 0)
      return kaleidoscope::EventHandlerResult::OK;
 
    for (uint8_t i=0;i<16;++i) {
      Focus.send(F("color"),i,colors[i],Focus.NEWLINE);
    }
    Focus.send(F("map\n"));
    for (uint8_t r=0;r<ROWS;++r) {
      for (uint8_t c=0;c<COLS;++c) {
        Focus.send(map[r][c]);
      }
      Focus.send(Focus.NEWLINE);
    }
 
    return kaleidoscope::EventHandlerResult::EVENT_CONSUMED;
  }
 
protected:
  void update(void) {
    for (uint8_t r = 0; r < ROWS; r++) {
      for (uint8_t c = 0; c < COLS; c++) {
        LEDControl.setCrgbAt(r, c, getColor(r,c));
      }
    }
  }
  void refreshAt(byte r, byte c) final {
    LEDControl.setCrgbAt(r, c, getColor(r,c));
  }
 
private:
  uint8_t current_index;
  enum { BOTH_HALVES, LEFT_HALF, RIGHT_HALF } which_half;
  cRGB colors[16];
  uint8_t map[ROWS][COLS];
  struct hsv { uint8_t h; uint8_t s; uint8_t v; } 
    hsv_colors[16];
 
  static inline uint8_t add_and_clamp(uint8_t value, int8_t delta) {
    int16_t result = ((int16_t)value) + delta;
    if (result < 0return 0;
    if (result > 255return 255;
    return result;
  }
  static inline uint8_t add_and_wrap(uint8_t value, int8_t delta) {
    int16_t result = ((int16_t)value) + delta;
    if (result < 0return result+255;
    if (result > 255return result-255;
    return result;
  }
 
  void updateCurrentColor(int8_t delta_h, int8_t delta_s, int8_t delta_v) {
    hsv& color = hsv_colors[current_index];
    color.h = add_and_wrap(color.h,delta_h);
    color.s = add_and_clamp(color.s,delta_s);
    color.v = add_and_clamp(color.v,delta_v);
 
    colors[current_index] = hsvToRgb(color.h,color.s,color.v);
  }
  
  bool is_active() {
    return LEDControl.get_mode() == this;
  }
 
  typedef enum { OFF, SHOW,
                 SWITCH_LEFT, SWITCH_RIGHT,
                 COLOR_1, COLOR_2, COLOR_3, COLOR_4,
                 COLOR_5, COLOR_6, COLOR_7, COLOR_8,
                 HUE_M10, HUE_M1, HUE_P1, HUE_P10,
                 SAT_M10, SAT_M1, SAT_P1, SAT_P10,
                 VAL_M10, VAL_M1, VAL_P1, VAL_P10 }
    picker_key;
 
  picker_key whichKey(byte row, byte col) {
    if (row == 3 && col == 6{
      return SWITCH_LEFT;
    }
    else if (row == 3 && col == 9{
      return SWITCH_RIGHT;
    }
    else if (row == 0 && col == 6{
      return OFF; // LED key, always off 
    }
 
    // non-editing half? show it 
    if (which_half == BOTH_HALVES ||
        (col < 8 && which_half == LEFT_HALF) ||
        (col >= 8 && which_half == RIGHT_HALF)) {
      return SHOW;
    }
 
    // pretend we're always showing the right half, by changing col if we're not 
    if (which_half == LEFT_HALF) {
      switch (col) {
      case 8: col=6break;
      case 9: col=7break;
      case 10: col=2break;
      case 11: col=3break;
      case 12: col=4break;
      case 13: col=5break;
      case 14: col=0break;
      case 15: col=1break;
      }
    }
 
    switch (col) {
    case 0:
      switch (row) {
      case 0:
        return COLOR_1;
      case 1:
        return COLOR_2;
      case 2:
        return COLOR_3;
      case 3:
        return COLOR_4;
      default:
        return OFF;
      };
    case 1:
      switch (row) {
      case 0:
        return COLOR_5;
      case 1:
        return COLOR_6;
      case 2:
        return COLOR_7;
      case 3:
        return COLOR_8;
      default:
        return OFF;
      };
    case 2:
      switch (row) {
      case 1:
        return HUE_M10;
      case 2:
        return SAT_M10;
      case 3:
        return VAL_M10;
      default:
        return OFF;
      };
    case 3:
      switch (row) {
      case 1:
        return HUE_M1;
      case 2:
        return SAT_M1;
      case 3:
        return VAL_M1;
      default:
        return OFF;
      };
    case 4:
      switch (row) {
      case 1:
        return HUE_P1;
      case 2:
        return SAT_P1;
      case 3:
        return VAL_P1;
      default:
        return OFF;
      };
    case 5:
      switch (row) {
      case 1:
        return HUE_P10;
      case 2:
        return SAT_P10;
      case 3:
        return VAL_P10;
      default:
        return OFF;
      };
    default:
      return OFF;
    };
 
    // won't be reached 
    return SHOW;
  }
  
  cRGB getColor(byte row, byte col) {
    picker_key key = whichKey(row,col);
    switch (key) {
    case SHOW:
      return colors[map[row][col]];
    case OFF:
    case SWITCH_LEFT:
    case SWITCH_RIGHT:
      return CRGB(0,0,0);
    case COLOR_1...COLOR_8:
      return colors[key - COLOR_1];
 
    case HUE_M10:
      return hsvToRgb(add_and_wrap(hsv_colors[current_index].h,-10),255,128);
    case HUE_M1:
      return hsvToRgb(add_and_wrap(hsv_colors[current_index].h,-1),255,128);
    case HUE_P1:
      return hsvToRgb(add_and_wrap(hsv_colors[current_index].h,+1),255,128);
    case HUE_P10:
      return hsvToRgb(add_and_wrap(hsv_colors[current_index].h,+10),255,128);
    case SAT_M10:
      return hsvToRgb(hsv_colors[current_index].h,50,128);
    case SAT_M1:
      return hsvToRgb(hsv_colors[current_index].h,100,128);
    case SAT_P1:
      return hsvToRgb(hsv_colors[current_index].h,150,128);
    case SAT_P10:
      return hsvToRgb(hsv_colors[current_index].h,200,128);
 
    case VAL_M10:
      return hsvToRgb(hsv_colors[current_index].h,128,50);
    case VAL_M1:
      return hsvToRgb(hsv_colors[current_index].h,128,100);
    case VAL_P1:
      return hsvToRgb(hsv_colors[current_index].h,128,150);
    case VAL_P10:
      return hsvToRgb(hsv_colors[current_index].h,128,200);
    };
 
    // won't be reached 
    return colors[map[row][col]];
  }
};
 
ColorPicker theColorPicker;