spiv: Add support for metadata viewer.
[gfxprim.git] / libs / core / GP_Color.c
blob2c42d4a15cc0d2ff16ce0a5a927059236a596ad6
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2011 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdint.h>
27 #include <string.h>
29 #include "GP_Convert.h"
31 #include "GP_Color.h"
34 static char *color_names[] = {
35 "Black",
36 "Red",
37 "Green",
38 "Blue",
39 "Yellow",
40 "Cyan",
41 "Magenta",
42 "Brown",
43 "Orange",
44 "DarkGray",
45 "LightGray",
46 "Purple",
47 "White"
50 static uint8_t rgb888_colors[][3] = {
51 {0x00, 0x00, 0x00}, /* Black */
52 {0xff, 0x00, 0x00}, /* Red */
53 {0x00, 0xff, 0x00}, /* Green */
54 {0x00, 0x00, 0xff}, /* Blue */
55 {0xff, 0xff, 0x00}, /* Yellow */
56 {0x00, 0xff, 0xff}, /* Cyan */
57 {0xff, 0x00, 0xff}, /* Magenta */
58 {0xa5, 0x2a, 0x2a}, /* Brown */
59 {0xff, 0xa5, 0x00}, /* Orange */
60 {0x7f, 0x7f, 0x7f}, /* DarkGray */
61 {0xbe, 0xbe, 0xbe}, /* LightGray */
62 {0xa0, 0x20, 0xf0}, /* Purple */
63 {0xff, 0xff, 0xff}, /* White */
66 /* 3-2-3 RGB palette */
67 static uint8_t p8_colors[] = {
68 0x00, /* Black */
69 0xe0, /* Red */
70 0x1c, /* Green */
71 0x03, /* Blue */
72 0xfc, /* Yellow */
73 0x1f, /* Cyan */
74 0xe7, /* Magenta */
75 0x88, /* Brown */
76 0xf0, /* Orange */
77 0x49, /* DarkGray */
78 0x92, /* LightGray */
79 0x8a, /* Purple */
80 0xff, /* White */
83 GP_Pixel GP_ColorToPixel(GP_Color color, GP_PixelType pixel_type)
85 GP_ASSERT(color < GP_COL_MAX);
86 GP_ASSERT(color >= 0);
88 if (pixel_type == GP_PIXEL_P8)
89 return p8_colors[color];
91 return GP_RGBToPixel(rgb888_colors[color][0],
92 rgb888_colors[color][1],
93 rgb888_colors[color][2],
94 pixel_type);
97 GP_Color GP_ColorNameToColor(const char *color_name)
99 unsigned int i;
101 for (i = 0; i < GP_COL_MAX; i++)
102 if (!strcasecmp(color_name, color_names[i]))
103 return i;
105 return -1;
108 const char *GP_ColorToColorName(GP_Color color)
110 if (color < 0 || color >= GP_COL_MAX)
111 return NULL;
113 return color_names[color];
116 bool GP_ColorNameToPixel(const char *color_name, GP_PixelType pixel_type,
117 GP_Pixel *pixel)
119 GP_Color color = GP_ColorNameToColor(color_name);
121 if (color == GP_COL_INVALID)
122 return false;
124 *pixel = GP_ColorToPixel(color, pixel_type);
126 return true;
129 void GP_ColorLoadPixels(GP_Pixel pixels[], GP_PixelType pixel_type)
131 unsigned int i;
133 for (i = 0; i < GP_COL_MAX; i++)
134 pixels[i] = GP_ColorToPixel(i, pixel_type);