Updated: Sudoku no longer crashes
[moon.git] / cairo / src / cairo-color.c
blob7640bf43621d22b893571d880c78ebf53eedaebd
1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2002 University of Southern California
4 * Copyright © 2005 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is University of Southern
32 * California.
34 * Contributor(s):
35 * Carl D. Worth <cworth@cworth.org>
38 #include "cairoint.h"
40 static cairo_color_t const cairo_color_white = {
41 1.0, 1.0, 1.0, 1.0,
42 0xffff, 0xffff, 0xffff, 0xffff
45 static cairo_color_t const cairo_color_black = {
46 0.0, 0.0, 0.0, 1.0,
47 0x0, 0x0, 0x0, 0xffff
50 static cairo_color_t const cairo_color_transparent = {
51 0.0, 0.0, 0.0, 0.0,
52 0x0, 0x0, 0x0, 0x0
55 static cairo_color_t const cairo_color_magenta = {
56 1.0, 0.0, 1.0, 1.0,
57 0xffff, 0x0, 0xffff, 0xffff
60 const cairo_color_t *
61 _cairo_stock_color (cairo_stock_t stock)
63 switch (stock) {
64 case CAIRO_STOCK_WHITE:
65 return &cairo_color_white;
66 case CAIRO_STOCK_BLACK:
67 return &cairo_color_black;
68 case CAIRO_STOCK_TRANSPARENT:
69 return &cairo_color_transparent;
72 ASSERT_NOT_REACHED;
74 /* If the user can get here somehow, give a color that indicates a
75 * problem. */
76 return &cairo_color_magenta;
79 void
80 _cairo_color_init (cairo_color_t *color)
82 *color = cairo_color_white;
85 void
86 _cairo_color_init_rgb (cairo_color_t *color,
87 double red, double green, double blue)
89 _cairo_color_init_rgba (color, red, green, blue, 1.0);
92 /* Convert a double in [0.0, 1.0] to an integer in [0, 65535]
93 * The conversion is designed to divide the input range into 65536
94 * equally-sized regions. This is achieved by multiplying by 65536 and
95 * then special-casing the result of an input value of 1.0 so that it
96 * maps to 65535 instead of 65536.
98 uint16_t
99 _cairo_color_double_to_short (double d)
101 uint32_t i;
102 i = (uint32_t) (d * 65536);
103 i -= (i >> 16);
104 return i;
107 static void
108 _cairo_color_compute_shorts (cairo_color_t *color)
110 color->red_short = _cairo_color_double_to_short (color->red * color->alpha);
111 color->green_short = _cairo_color_double_to_short (color->green * color->alpha);
112 color->blue_short = _cairo_color_double_to_short (color->blue * color->alpha);
113 color->alpha_short = _cairo_color_double_to_short (color->alpha);
116 void
117 _cairo_color_init_rgba (cairo_color_t *color,
118 double red, double green, double blue,
119 double alpha)
121 color->red = red;
122 color->green = green;
123 color->blue = blue;
124 color->alpha = alpha;
126 _cairo_color_compute_shorts (color);
129 void
130 _cairo_color_multiply_alpha (cairo_color_t *color,
131 double alpha)
133 color->alpha *= alpha;
135 _cairo_color_compute_shorts (color);
138 void
139 _cairo_color_get_rgba (cairo_color_t *color,
140 double *red,
141 double *green,
142 double *blue,
143 double *alpha)
145 *red = color->red;
146 *green = color->green;
147 *blue = color->blue;
148 *alpha = color->alpha;
151 void
152 _cairo_color_get_rgba_premultiplied (cairo_color_t *color,
153 double *red,
154 double *green,
155 double *blue,
156 double *alpha)
158 *red = color->red * color->alpha;
159 *green = color->green * color->alpha;
160 *blue = color->blue * color->alpha;
161 *alpha = color->alpha;
164 cairo_bool_t
165 _cairo_color_equal (const cairo_color_t *color_a,
166 const cairo_color_t *color_b)
168 if (color_a == color_b)
169 return TRUE;
171 return color_a->red_short == color_b->red_short &&
172 color_a->green_short == color_b->green_short &&
173 color_a->blue_short == color_b->blue_short &&
174 color_a->alpha_short == color_b->alpha_short;