summaryrefslogtreecommitdiffstats
path: root/apps/gui/color_picker.c
blob: eb4490035db82e7d0d0f2b4d396ee34d60725f0e (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) Jonathan Gordon (2006)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "stdarg.h"
#include "string.h"
#include "stdio.h"
#include "kernel.h"
#include "system.h"
#include "screen_access.h"
#include "font.h"
#include "debug.h"
#include "misc.h"
#include "settings.h"
#include "scrollbar.h"
#include "lang.h"
#include "splash.h"
#include "action.h"
#include "icon.h"
#include "color_picker.h"

/* structure for color info */
struct rgb_pick
{
    unsigned color;                 /* native color value            */
    union
    {
        unsigned char rgb_val[6];   /* access to components as array */
        struct
        {
            unsigned char r;        /* native red value              */
            unsigned char g;        /* native green value            */
            unsigned char b;        /* native blue value             */
            unsigned char red;      /* 8 bit red value               */
            unsigned char green;    /* 8 bit green value             */
            unsigned char blue;     /* 8 bit blue value              */
        } __attribute__ ((__packed__)); /* assume byte packing       */
    };
};


/* list of primary colors */
#define SB_PRIM 0
#define SB_FILL 1
static const fb_data prim_rgb[][3] =
{
    /* Foreground colors for sliders */
    {
        LCD_RGBPACK(255,   0,   0),
        LCD_RGBPACK(  0, 255,   0),
        LCD_RGBPACK(  0,   0, 255),
    },
    /* Fill colors for sliders */
    {
        LCD_RGBPACK( 85,   0,   0),
        LCD_RGBPACK(  0,  85,   0),
        LCD_RGBPACK(  0,   0,  85),
    },
};

/* maximum values for components */
static const unsigned char rgb_max[3] =
{
    LCD_MAX_RED,
    LCD_MAX_GREEN,
    LCD_MAX_BLUE
};

/* Unpacks the color value into native rgb values and 24 bit rgb values */
static void unpack_rgb(struct rgb_pick *rgb)
{
    unsigned color = _LCD_UNSWAP_COLOR(rgb->color);
    rgb->red   = _RGB_UNPACK_RED(color);
    rgb->green = _RGB_UNPACK_GREEN(color);
    rgb->blue  = _RGB_UNPACK_BLUE(color);
    rgb->r     = _RGB_UNPACK_RED_LCD(color);
    rgb->g     = _RGB_UNPACK_GREEN_LCD(color);
    rgb->b     = _RGB_UNPACK_BLUE_LCD(color);
}

/* Packs the native rgb colors into a color value */
static inline void pack_rgb(struct rgb_pick *rgb)
{
    rgb->color = LCD_RGBPACK_LCD(rgb->r, rgb->g, rgb->b);
}

/* Returns LCD_BLACK if the color is above a threshold brightness
   else return LCD_WHITE */
static inline unsigned get_black_or_white(const struct rgb_pick *rgb)
{
    return (2*rgb->red + 5*rgb->green + rgb->blue) >= 1024 ?
        LCD_BLACK : LCD_WHITE;
}

#define MARGIN_LEFT             0 /* Left margin of screen                */
#define MARGIN_TOP              2 /* Top margin of screen                 */
#define MARGIN_RIGHT            0 /* Right margin of screen               */
#define MARGIN_BOTTOM           6 /* Bottom margin of screen              */
#define SLIDER_MARGIN_LEFT      2 /* Gap to left of sliders               */
#define SLIDER_MARGIN_RIGHT     2 /* Gap to right of sliders              */
#define TITLE_MARGIN_BOTTOM     4 /* Space below title bar                */
#define SELECTOR_LR_MARGIN      0 /* Margin between ">" and text          */
#define SELECTOR_TB_MARGIN      1 /* Margin on top and bottom of selector */
#define SWATCH_TOP_MARGIN       4 /* Space between last slider and swatch */
#define SELECTOR_WIDTH          get_icon_width(display->screen_type)
#define SELECTOR_HEIGHT         8 /* Height of > and < bitmaps            */

/* dunno why lcd_set_drawinfo should be left out of struct screen */
static void set_drawinfo(struct screen *display, int mode,
                         unsigned foreground, unsigned background)
{
    display->set_drawmode(mode);
    if (display->depth > 1)
    {
        display->set_foreground(foreground);
        display->set_background(background);
    }
}

static void draw_screen(struct screen *display, char *title,
                        struct rgb_pick *rgb, int row)
{
    unsigned  text_color       = LCD_BLACK;
    unsigned  background_color = LCD_WHITE;
    char      buf[32];
    int       i, x, y;
    int       text_top;
    int       slider_left, slider_width;
    bool      display_three_rows;
    int       max_label_width;

    display->clear_display();

    if (display->depth > 1)
    {
        text_color       = display->get_foreground();
        background_color = display->get_background();
    }

    /* Find out if there's enough room for three sliders or just
       enough to display the selected slider - calculate total height
       of display with three sliders present */
    display_three_rows =
        display->getheight() >=
            MARGIN_TOP                 +
            display->getcharheight()*4 + /* Title + 3 sliders */
            TITLE_MARGIN_BOTTOM        +
            SELECTOR_TB_MARGIN*6       + /* 2 margins/slider  */
            MARGIN_BOTTOM;

    /* Figure out widest label character in case they vary -
       this function assumes labels are one character */
    for (i = 0, max_label_width = 0; i < 3; i++)
    {
        buf[0] = str(LANG_COLOR_RGB_LABELS)[i];
        buf[1] = '\0';
        x = display->getstringsize(buf, NULL, NULL);
        if (x > max_label_width)
            max_label_width = x;
    }

    /* Draw title string */
    set_drawinfo(display, DRMODE_SOLID, text_color, background_color);
    display->getstringsize(title, &x, &y);
    display->putsxy((display->getwidth() - x) / 2, MARGIN_TOP, title);

    /* Get slider positions and top starting position */
    text_top     = MARGIN_TOP + y + TITLE_MARGIN_BOTTOM + SELECTOR_TB_MARGIN;
    slider_left  = MARGIN_LEFT + SELECTOR_WIDTH + SELECTOR_LR_MARGIN +
                   max_label_width + SLIDER_MARGIN_LEFT;
    slider_width = display->getwidth() - slider_left - SLIDER_MARGIN_RIGHT -
                   display->getcharwidth()*2 - SELECTOR_LR_MARGIN -
                   SELECTOR_WIDTH - MARGIN_RIGHT;

    for (i = 0; i < 3; i++)
    {
        unsigned sb_flags = HORIZONTAL;
        int      mode     = DRMODE_SOLID;
        unsigned fg       = text_color;
        unsigned bg       = background_color;

        if (!display_three_rows)
            i = row;

        if (i == row)
        {
            set_drawinfo(display, DRMODE_SOLID, text_color,
                         background_color);

            if (global_settings.cursor_style != 0)
            {
                /* Draw solid bar selection bar */
                display->fillrect(0,
                                  text_top - SELECTOR_TB_MARGIN,
                                  display->getwidth(),
                                  display->getcharheight() +
                                  SELECTOR_TB_MARGIN*2);

                if (display->depth < 16)
                {
                    sb_flags |= FOREGROUND | INNER_FILL;
                    mode     |= DRMODE_INVERSEVID;
                }
            }
            else if (display_three_rows)
            {
                /* Draw ">    <" around sliders */
                int top = text_top + (display->getcharheight() -
                                      SELECTOR_HEIGHT) / 2;
                screen_put_iconxy(display, MARGIN_LEFT, top, Icon_Cursor);
                screen_put_iconxy(display, 
                                  display->getwidth() - MARGIN_RIGHT -
                                          get_icon_width(display->screen_type),
                                          top, Icon_Cursor);
            }

            if (display->depth >= 16)
            {
                sb_flags |= FOREGROUND | INNER_BGFILL;
                mode      = DRMODE_FG;
                fg        = prim_rgb[SB_PRIM][i];
                bg        = prim_rgb[SB_FILL][i];
            }
        }

        set_drawinfo(display, mode, fg, bg);

        /* Draw label */
        buf[0] = str(LANG_COLOR_RGB_LABELS)[i];
        buf[1] = '\0';
        display->putsxy(slider_left - display->getcharwidth() -
                        SLIDER_MARGIN_LEFT, text_top, buf);

        /* Draw color value */
        snprintf(buf, 3, "%02d", rgb->rgb_val[i]);
        display->putsxy(slider_left + slider_width + SLIDER_MARGIN_RIGHT,
                        text_top, buf);

        /* Draw scrollbar */
        gui_scrollbar_draw(display,
                           slider_left,
                           text_top + display->getcharheight() / 4,
                           slider_width,
                           display->getcharheight() / 2,
                           rgb_max[i],
                           0,
                           rgb->rgb_val[i],
                           sb_flags);

        /* Advance to next line */
        text_top += display->getcharheight() + 2*SELECTOR_TB_MARGIN;

        if (!display_three_rows)
            break;
    } /* end for */

    /* Format RGB: #rrggbb */
    snprintf(buf, sizeof(buf), str(LANG_COLOR_RGB_VALUE),
                               rgb->red, rgb->green, rgb->blue);

    if (display->depth >= 16)
    {
        /* Display color swatch on color screens only */
        int left   = MARGIN_LEFT + SELECTOR_WIDTH + SELECTOR_LR_MARGIN;
        int top    = text_top + SWATCH_TOP_MARGIN;
        int width  = display->getwidth() - left - SELECTOR_LR_MARGIN -
                     SELECTOR_WIDTH - MARGIN_RIGHT;
        int height = display->getheight() - top - MARGIN_BOTTOM;

        /* Only draw if room */
        if (height >= display->getcharheight() + 2)
        {
            display->set_foreground(rgb->color);
            display->fillrect(left, top, width, height);

            /* Draw RGB: #rrggbb in middle of swatch */
            display->set_drawmode(DRMODE_FG);
            display->getstringsize(buf, &x, &y);
            display->set_foreground(get_black_or_white(rgb));

            x = left + (width  - x) / 2;
            y = top  + (height - y) / 2;

            display->putsxy(x, y, buf);
            display->set_drawmode(DRMODE_SOLID);

            /* Draw border */
            display->set_foreground(text_color);
            display->drawrect(left, top, width, height);
        }
    }
    else
    {
        /* Display RGB value only centered on remaining display if room */
        display->getstringsize(buf, &x, &y);
        i = text_top + SWATCH_TOP_MARGIN;

        if (i + y <= display->getheight() - MARGIN_BOTTOM)
        {
            set_drawinfo(display, DRMODE_SOLID, text_color, background_color);
            x = (display->getwidth() - x) / 2;
            y = (i + display->getheight() - MARGIN_BOTTOM - y) / 2;
            display->putsxy(x, y, buf);
        }
    }

    display->setfont(FONT_UI);

    display->update();
    /* Be sure screen mode is reset */
    set_drawinfo(display, DRMODE_SOLID, text_color, background_color);
}

#ifdef HAVE_TOUCHSCREEN
static int touchscreen_slider(struct rgb_pick *rgb, int *selected_slider)
{
    short     x,y;
    int       text_top,i,x1;
    int       slider_left, slider_width;
    unsigned  button = action_get_touchscreen_press(&x, &y);
    bool      display_three_rows;
    int       max_label_width;
    struct screen *display = &screens[SCREEN_MAIN];
    int      pressed_slider;
    char buf[2];
    
    if (button == BUTTON_NONE)
        return ACTION_NONE;
    /* same logic as draw_screen */
    /* Figure out widest label character in case they vary -
       this function assumes labels are one character */
    for (i = 0, max_label_width = 0; i < 3; i++)
    {
        buf[0] = str(LANG_COLOR_RGB_LABELS)[i];
        buf[1] = '\0';
        x1 = display->getstringsize(buf, NULL, NULL);
        if (x1 > max_label_width)
            max_label_width = x1;
    }
    /* Get slider positions and top starting position */
    text_top     = MARGIN_TOP + display->getcharheight() + TITLE_MARGIN_BOTTOM +
                   SELECTOR_TB_MARGIN;
    slider_left  = MARGIN_LEFT + SELECTOR_WIDTH + SELECTOR_LR_MARGIN +
                   max_label_width + SLIDER_MARGIN_LEFT;
    slider_width = display->getwidth() - slider_left - SLIDER_MARGIN_RIGHT -
                   display->getcharwidth()*2 - SELECTOR_LR_MARGIN -
                   SELECTOR_WIDTH - MARGIN_RIGHT;
    display_three_rows =
        display->getheight() >=
            MARGIN_TOP                 +
            display->getcharheight()*4 + /* Title + 3 sliders */
            TITLE_MARGIN_BOTTOM        +
            SELECTOR_TB_MARGIN*6       + /* 2 margins/slider  */
            MARGIN_BOTTOM;
    if (y < MARGIN_TOP+display->getcharheight())
    {
        if (button == BUTTON_REL)
            return ACTION_STD_CANCEL;
    }
    y -= text_top;
    pressed_slider = y/display->char_height;
    if (pressed_slider > (display_three_rows?2:0))
    {
        if (button == BUTTON_REL)
            return ACTION_STD_OK;
    }
    if (pressed_slider != *selected_slider)
        *selected_slider = pressed_slider;
    if (x < slider_left+slider_width && 
        x > slider_left)
    {
        x -= slider_left;
        rgb->rgb_val[pressed_slider] = 
            (x*rgb_max[pressed_slider]/(slider_width-slider_left));
        pack_rgb(rgb);
    }
    return ACTION_NONE;
}
#endif
/***********
 set_color
 returns true if USB was inserted, false otherwise
 color is a pointer to the colour (in native format) to modify
 set banned_color to -1 to allow all
 ***********/
bool set_color(struct screen *display, char *title, unsigned *color,
               unsigned banned_color)
{
    int exit = 0, slider = 0;
    struct rgb_pick rgb;

    rgb.color = *color;

    while (!exit)
    {
        int button;

        unpack_rgb(&rgb);

        if (display != NULL)
        {
            draw_screen(display, title, &rgb, slider);
        }
        else
        {
            int i;
            FOR_NB_SCREENS(i)
                draw_screen(&screens[i], title, &rgb, slider);
        }

        button = get_action(CONTEXT_SETTINGS_COLOURCHOOSER, TIMEOUT_BLOCK);
#ifdef HAVE_TOUCHSCREEN
        if (button == ACTION_TOUCHSCREEN)
            button = touchscreen_slider(&rgb, &slider);
#endif

        switch (button)
        {
            case ACTION_STD_PREV:
            case ACTION_STD_PREVREPEAT:
                slider = (slider + 2) % 3;
                break;

            case ACTION_STD_NEXT:
            case ACTION_STD_NEXTREPEAT:
                slider = (slider + 1) % 3;
                break;

            case ACTION_SETTINGS_INC:
            case ACTION_SETTINGS_INCREPEAT:
                if (rgb.rgb_val[slider] < rgb_max[slider])
                    rgb.rgb_val[slider]++;
                pack_rgb(&rgb);
                break;

            case ACTION_SETTINGS_DEC:
            case ACTION_SETTINGS_DECREPEAT:
                if (rgb.rgb_val[slider] > 0)
                    rgb.rgb_val[slider]--;
                pack_rgb(&rgb);
                break;

            case ACTION_STD_OK:
                if (banned_color != (unsigned)-1 &&
                    banned_color == rgb.color)
                {
                    splash(HZ*2, ID2P(LANG_COLOR_UNACCEPTABLE));
                    break;
                }
                *color = rgb.color;
                exit = 1;
                break;

            case ACTION_STD_CANCEL:
                exit = 1;
                break;

            default:
                if (default_event_handler(button) == SYS_USB_CONNECTED)
                    return true;
                break;
        }
    }

    return false;
}