2 * Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
27 color_set(struct color *color,
28 unsigned int red, unsigned int green, unsigned int blue)
34 color->f_red = ((double)color->red) / 65535;
35 color->f_green = ((double)color->green) / 65535;
36 color->f_blue = ((double)color->blue) / 65535;
39 struct color *color_new(unsigned int red, unsigned int green, unsigned int blue)
41 struct color *color = malloc(sizeof(struct color));
43 color_set(color, red, green, blue);
48 struct color *color_dup(struct color *color)
50 return color_new(color->red, color->green, color->blue);
53 int is_color(const char *str)
58 if (n != 13 || str[0] != '#')
61 for (i = 1; i < n; i++)
62 if (isxdigit(str[i]) == 0)
68 struct color *string_to_color(const char *str)
71 unsigned int red, green, blue;
76 strncpy(tmp, str + 1, 4);
78 red = strtol(tmp, NULL, 16);
80 strncpy(tmp, str + 5, 4);
82 green = strtol(tmp, NULL, 16);
84 strncpy(tmp, str + 9, 4);
86 blue = strtol(tmp, NULL, 16);
88 return color_new(red, green, blue);
91 char *color_to_string(struct color *color)
93 char *str = malloc(1 + 12 + 1);
95 sprintf(str, "#%.4x%.4x%.4x", color->red, color->green, color->blue);