Show "unchanged color" as checkered pattern
[colorize.git] / gui / palette.c
1 #include <gtk/gtk.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "main.h"
5 #include "../src/mark.h"
6 #include "../src/yuv.h"
7 #include "palette.h"
8 #include "timeline.h"
9
10 int mark_selected = 0;
11
12 #define COLOR_WIDTH 100
13 #define COLOR_HEIGHT 20
14
15 char *bc_pic[] = {
16         "     *     ",
17         " *   *   * ",
18         "  *     *  ",
19         "    ***    ",
20         "   *** *   ",
21         "** *** * **",
22         "   *** *   ",
23         "    ***    ",
24         "  *     *  ",
25         " *   *   * ",
26         "     *     ",
27 };
28 static GdkPixbuf *draw_pixpuf(int i)
29 {
30         GdkPixbuf *pixbuf;
31         guchar *data;
32         int j, k, rs;
33         int cr, cg, cb;
34         float bright, contrast;
35         double r, g, b, y, u, v;
36
37         cr = mark_palette[i].r;
38         cg = mark_palette[i].g;
39         cb = mark_palette[i].b;
40         bright = mark_palette[i].bright;
41         contrast = mark_palette[i].contrast;
42         r = cr / 255.0;
43         g = cg / 255.0;
44         b = cb / 255.0;
45         rgb2yuv_pixle(r, g, b, &y, &u, &v);
46
47         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, COLOR_WIDTH, COLOR_HEIGHT);
48         data = gdk_pixbuf_get_pixels(pixbuf);
49         rs = gdk_pixbuf_get_rowstride(pixbuf);
50         if (cr == 255 && cg == 255 && cb == 255) {
51                 for (k = 0; k < COLOR_WIDTH; k++) {
52                         for (j = 0; j < COLOR_HEIGHT; j++) {
53                                 data[j * rs + k * 3 + 0] = 255;
54                                 data[j * rs + k * 3 + 1] = 255;
55                                 data[j * rs + k * 3 + 2] = 255;
56                         }
57                 }
58         } else {
59                 for (k = 0; k < COLOR_WIDTH; k++) {
60                         /* we use height as horizontal size of original color area */
61                         if (k >= COLOR_HEIGHT+3) {
62                                 y = (double)(k-COLOR_HEIGHT-3) / (double)((COLOR_WIDTH-COLOR_HEIGHT-3)-1);
63                                 y = (y - 0.5) * contrast + 0.5;
64                                 y += bright;
65                                 if (y < 0)
66                                         y = 0;
67                                 if (y > 1)
68                                         y = 1;
69                                 yuv2rgb_pixle(y, u, v, &r, &g, &b);
70                                 cr = r * 255.0F;
71                                 if (cr < 0)
72                                         cr = 0;
73                                 else if (cr > 255)
74                                         cr = 255;
75                                 cg = g * 255.0F;
76                                 if (cg < 0)
77                                         cg = 0;
78                                 else if (cg > 255)
79                                         cg = 255;
80                                 cb = b * 255.0F;
81                                 if (cb < 0)
82                                         cb = 0;
83                                 else if (cb > 255)
84                                         cb = 255;
85                         } else
86                         if (k >= COLOR_HEIGHT) {
87                                 cr = 255;
88                                 cg = 255;
89                                 cb = 255;
90                         }
91                         for (j = 0; j < COLOR_HEIGHT; j++) {
92                                 data[j * rs + k * 3 + 0] = cr;
93                                 data[j * rs + k * 3 + 1] = cg;
94                                 data[j * rs + k * 3 + 2] = cb;
95                         }
96                 }
97         }
98
99         /* indicate a change in brightness + contrast */
100         if (bright != 0 || contrast != 1) {
101                 for (k = 0; k < 11; k++) {
102                         for (j = 0; j < 11; j++) {
103                                 if (bc_pic[j][k] == '*') {
104                                         data[j * rs + k * 3 + 0] = 255;
105                                         data[j * rs + k * 3 + 1] = 0;
106                                         data[j * rs + k * 3 + 2] = 0;
107                                 }
108                         }
109                 }
110         }
111
112         return pixbuf;
113 }
114
115 void create_palette(void)
116 {
117         GtkListStore *palette_store;
118         GtkTreeIter   iter;
119         int i;
120         char number[10];
121         GdkPixbuf *color;
122
123         for (i = 0; i < 255; i++) {
124                 strcpy(mark_palette[i].name, "");
125                 mark_palette[i].r = 0;
126                 mark_palette[i].g = 0;
127                 mark_palette[i].b = 0;
128                 mark_palette[i].bright = 0;
129                 mark_palette[i].contrast = 1;
130         }
131
132         strcpy(mark_palette[0].name, "red");
133         mark_palette[0].r = 255;
134         mark_palette[0].g = 0;
135         mark_palette[0].b = 0;
136         strcpy(mark_palette[1].name, "green");
137         mark_palette[1].r = 0;
138         mark_palette[1].g = 255;
139         mark_palette[1].b = 0;
140         strcpy(mark_palette[2].name, "blue");
141         mark_palette[2].r = 0;
142         mark_palette[2].g = 0;
143         mark_palette[2].b = 255;
144
145         palette_store = gtk_list_store_new(3, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING);
146         gtk_tree_view_set_model(GTK_TREE_VIEW(palette_treeview), GTK_TREE_MODEL(palette_store));
147         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(palette_treeview), FALSE);
148
149         for (i = 0; i < 255; i++) {
150                 gtk_list_store_append(palette_store, &iter);
151                 sprintf(number, "%d", i+1);
152                 color = draw_pixpuf(i);
153                 gtk_list_store_set(palette_store, &iter, 0, number, 1, color, 2, mark_palette[i].name, -1);
154                 g_object_unref(color);
155         }
156
157         g_object_unref(palette_store);
158
159 }
160
161 void update_color(int entry)
162 {
163         GdkPixbuf *color;
164
165         color = draw_pixpuf(entry);
166
167         GtkTreeIter iter;
168         GtkTreeModel *model;
169         char path[10];
170
171         sprintf(path, "%d", entry);
172         model = gtk_tree_view_get_model(GTK_TREE_VIEW(palette_treeview));
173         if (gtk_tree_model_get_iter_from_string(model, &iter, path)) {
174                 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, color, 2, mark_palette[entry].name, -1);
175         }
176 }
177