source: libpipi/trunk/pipi/colorstring.c @ 2844

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 12 years ago

Fix headers.

File size: 4.1 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org
5 *                All Rights Reserved
6 *
7 *  $Id$
8 *
9 *  This library is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16/*
17 * color.c: color parsing functions
18 */
19
20#include "config.h"
21#include "common.h"
22
23/*
24 *  libpipi       Pathetic image processing interface library
25 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
26 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org
27 *                All Rights Reserved
28 *
29 *  $Id$
30 *
31 *  This library is free software. It comes without any warranty, to
32 *  the extent permitted by applicable law. You can redistribute it
33 *  and/or modify it under the terms of the Do What The Fuck You Want
34 *  To Public License, Version 2, as published by Sam Hocevar. See
35 *  http://sam.zoy.org/wtfpl/COPYING for more details.
36 */
37
38/*
39 * colorstring.c: color from string functions
40 */
41
42#include "config.h"
43#include "common.h"
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <stdarg.h>
48#include <stdint.h>
49#include <string.h>
50
51#include "pipi.h"
52#include "pipi_internals.h"
53
54
55struct color_table
56{
57    char name[255];
58    float a,r,g,b;
59};
60
61struct color_table color_table[] =
62{
63    {.name = "black"  , 1,   0,    0,    0},
64    {.name = "white"  , 1,   1,    1,    1},
65    {.name = "red"    , 1,   1,    0,    0},
66    {.name = "green"  , 1,   0,    1,    0},
67    {.name = "blue"   , 1,   0,    0,    1},
68    {.name = "yellow" , 1,   1,    1,    0},
69    {.name = "cyan"   , 1,   0,    1,    1},
70    {.name = "magenta", 1,   1,    0,    1},
71    {.name = "grey"   , 1,   0.5,  0.5,  0.5},
72    {.name = "gray"   , 1,   0.5,  0.5,  0.5},
73    {.name = "grey50" , 1,   0.5,  0.5,  0.5},
74    {.name = "gray50" , 1,   0.5,  0.5,  0.5},
75    {.name = "grey25" , 1,   0.25, 0.25, 0.25},
76    {.name = "gray25" , 1,   0.25, 0.25, 0.25},
77};
78
79
80
81pipi_pixel_t *pipi_get_color_from_string(const char* s)
82{
83    if(!s) return NULL;
84
85
86    pipi_pixel_t *color = malloc(sizeof(pipi_pixel_t));
87
88
89    if(s[0] == '#')
90    {
91        uint32_t c = 0;
92        sscanf(s, "%x", &c);
93
94        color->pixel_float.a = ((c&0xFF000000)>>24) / 255.0f;
95        color->pixel_float.r = ((c&0x00FF0000)>>16) / 255.0f;
96        color->pixel_float.g = ((c&0x0000FF00)>>8)  / 255.0f;
97        color->pixel_float.b = ((c&0x000000FF)>>0)  / 255.0f;
98    }
99    else if(!strncmp(s, "rgb(", 4))
100    {
101        uint32_t r ,g ,b;
102        sscanf(s, "rgb(%u,%u,%u)", &r, &g, &b);
103        color->pixel_float.r = r / 255.0f;
104        color->pixel_float.g = g / 255.0f;
105        color->pixel_float.b = b / 255.0f;
106    }
107    else if(!strncmp(s, "frgb(", 5))
108    {
109        float r ,g ,b;
110        sscanf(s, "frgb(%f,%f,%f)", &r, &g, &b);
111        color->pixel_float.r = r;
112        color->pixel_float.g = g;
113        color->pixel_float.b = b;
114    }
115    else if(!strncmp(s, "argb(", 4))
116    {
117        uint32_t a, r ,g ,b;
118        sscanf(s, "argb(%u,%u,%u,%u)", &a, &r, &g, &b);
119        color->pixel_float.a = a / 255.0f;
120        color->pixel_float.r = r / 255.0f;
121        color->pixel_float.g = g / 255.0f;
122        color->pixel_float.b = b / 255.0f;
123    }
124    else if(!strncmp(s, "fargb(", 5))
125    {
126        float a, r ,g ,b;
127        sscanf(s, "fargb(%f, %f,%f,%f)", &a, &r, &g, &b);
128        color->pixel_float.a = a;
129        color->pixel_float.r = r;
130        color->pixel_float.g = g;
131        color->pixel_float.b = b;
132    }
133    else
134    {
135        unsigned int i;
136        for(i=0; i<sizeof(color_table); i++)
137        {
138            if(!strcasecmp(color_table[i].name,s))
139            {
140                color->pixel_float.a = color_table[i].a;
141                color->pixel_float.r = color_table[i].r;
142                color->pixel_float.g = color_table[i].g;
143                color->pixel_float.b = color_table[i].b;
144                break;
145            }
146        }
147
148
149    }
150    return color;
151}
Note: See TracBrowser for help on using the repository browser.