replaced usage of deprecated gtk function gtk_color_button_XXX by new gtk_color_choos...
[psensor.git] / src / lib / color.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
23
24 #include "color.h"
25
26 void color_set(struct color *c, unsigned int r, unsigned int g, unsigned int b)
27 {
28         c->red = r;
29         c->green = g;
30         c->blue = b;
31
32         c->f_red = ((double)r) / 65535;
33         c->f_green = ((double)g) / 65535;
34         c->f_blue = ((double)b) / 65535;
35 }
36
37 void color_set_f(struct color *c, double r, double g, double b)
38 {
39         c->f_red = r;
40         c->f_green = g;
41         c->f_blue = b;
42
43         c->red = 65535 * r;
44         c->green = 65535 * g;
45         c->blue = 65535 * b;
46 }
47
48 struct color *color_new(unsigned int red, unsigned int green, unsigned int blue)
49 {
50         struct color *color = malloc(sizeof(struct color));
51
52         color_set(color, red, green, blue);
53
54         return color;
55 }
56
57 struct color *color_dup(struct color *color)
58 {
59         return color_new(color->red, color->green, color->blue);
60 }
61
62 int is_color(const char *str)
63 {
64         int n = strlen(str);
65         int i;
66
67         if (n != 13 || str[0] != '#')
68                 return 0;
69
70         for (i = 1; i < n; i++)
71                 if (isxdigit(str[i]) == 0)
72                         return 0;
73
74         return 1;
75 }
76
77 struct color *str_to_color(const char *str)
78 {
79         char tmp[5];
80         unsigned int red, green, blue;
81
82         if (!is_color(str))
83                 return NULL;
84
85         strncpy(tmp, str + 1, 4);
86         tmp[4] = '\0';
87         red = strtol(tmp, NULL, 16);
88
89         strncpy(tmp, str + 5, 4);
90         tmp[4] = '\0';
91         green = strtol(tmp, NULL, 16);
92
93         strncpy(tmp, str + 9, 4);
94         tmp[4] = '\0';
95         blue = strtol(tmp, NULL, 16);
96
97         return color_new(red, green, blue);
98 }
99
100 char *color_to_str(const struct color *color)
101 {
102         char *str = malloc(1 + 12 + 1);
103
104         sprintf(str, "#%.4x%.4x%.4x", color->red, color->green, color->blue);
105
106         return str;
107 }