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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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
35 * Carl D. Worth <cworth@cworth.org>
40 static cairo_color_t const cairo_color_white = {
42 0xffff, 0xffff, 0xffff, 0xffff
45 static cairo_color_t const cairo_color_black = {
50 static cairo_color_t const cairo_color_transparent = {
55 static cairo_color_t const cairo_color_magenta = {
57 0xffff, 0x0, 0xffff, 0xffff
61 _cairo_stock_color (cairo_stock_t 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;
71 case CAIRO_STOCK_NUM_COLORS:
74 /* If the user can get here somehow, give a color that indicates a
76 return &cairo_color_magenta;
80 /* Convert a double in [0.0, 1.0] to an integer in [0, 65535]
81 * The conversion is designed to divide the input range into 65536
82 * equally-sized regions. This is achieved by multiplying by 65536 and
83 * then special-casing the result of an input value of 1.0 so that it
84 * maps to 65535 instead of 65536.
87 _cairo_color_double_to_short (double d)
90 i = (uint32_t) (d * 65536);
96 _cairo_color_compute_shorts (cairo_color_t *color)
98 color->red_short = _cairo_color_double_to_short (color->red * color->alpha);
99 color->green_short = _cairo_color_double_to_short (color->green * color->alpha);
100 color->blue_short = _cairo_color_double_to_short (color->blue * color->alpha);
101 color->alpha_short = _cairo_color_double_to_short (color->alpha);
105 _cairo_color_init_rgba (cairo_color_t *color,
106 double red, double green, double blue,
110 color->green = green;
112 color->alpha = alpha;
114 _cairo_color_compute_shorts (color);
118 _cairo_color_multiply_alpha (cairo_color_t *color,
121 color->alpha *= alpha;
123 _cairo_color_compute_shorts (color);
127 _cairo_color_get_rgba (cairo_color_t *color,
134 *green = color->green;
136 *alpha = color->alpha;
140 _cairo_color_get_rgba_premultiplied (cairo_color_t *color,
146 *red = color->red * color->alpha;
147 *green = color->green * color->alpha;
148 *blue = color->blue * color->alpha;
149 *alpha = color->alpha;
152 /* NB: This function works both for unmultiplied and premultiplied colors */
154 _cairo_color_equal (const cairo_color_t *color_a,
155 const cairo_color_t *color_b)
157 if (color_a == color_b)
160 if (color_a->alpha_short != color_b->alpha_short)
163 if (color_a->alpha_short == 0)
166 return color_a->red_short == color_b->red_short &&
167 color_a->green_short == color_b->green_short &&
168 color_a->blue_short == color_b->blue_short;
172 _cairo_color_stop_equal (const cairo_color_stop_t *color_a,
173 const cairo_color_stop_t *color_b)
175 if (color_a == color_b)
178 return color_a->alpha_short == color_b->alpha_short &&
179 color_a->red_short == color_b->red_short &&
180 color_a->green_short == color_b->green_short &&
181 color_a->blue_short == color_b->blue_short;
185 _cairo_color_get_content (const cairo_color_t *color)
187 if (CAIRO_COLOR_IS_OPAQUE (color))
188 return CAIRO_CONTENT_COLOR;
190 if (color->red_short == 0 &&
191 color->green_short == 0 &&
192 color->blue_short == 0)
194 return CAIRO_CONTENT_ALPHA;
197 return CAIRO_CONTENT_COLOR_ALPHA;