1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2002 University of Southern California
4
 * Copyright © 2005 Red Hat, Inc.
5
 *
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.
13
 *
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
19
 *
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/
24
 *
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.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is University of Southern
32
 * California.
33
 *
34
 * Contributor(s):
35
 *	Carl D. Worth <cworth@cworth.org>
36
 */
37

            
38
#include "cairoint.h"
39

            
40
static cairo_color_t const cairo_color_white = {
41
    1.0,    1.0,    1.0,    1.0,
42
    0xffff, 0xffff, 0xffff, 0xffff
43
};
44

            
45
static cairo_color_t const cairo_color_black = {
46
    0.0, 0.0, 0.0, 1.0,
47
    0x0, 0x0, 0x0, 0xffff
48
};
49

            
50
static cairo_color_t const cairo_color_transparent = {
51
    0.0, 0.0, 0.0, 0.0,
52
    0x0, 0x0, 0x0, 0x0
53
};
54

            
55
static cairo_color_t const cairo_color_magenta = {
56
    1.0,    0.0, 1.0,    1.0,
57
    0xffff, 0x0, 0xffff, 0xffff
58
};
59

            
60
const cairo_color_t *
61
1058556
_cairo_stock_color (cairo_stock_t stock)
62
{
63
1058556
    switch (stock) {
64
1157
    case CAIRO_STOCK_WHITE:
65
1157
	return &cairo_color_white;
66
823795
    case CAIRO_STOCK_BLACK:
67
823795
	return &cairo_color_black;
68
233604
    case CAIRO_STOCK_TRANSPARENT:
69
233604
	return &cairo_color_transparent;
70

            
71
    case CAIRO_STOCK_NUM_COLORS:
72
    default:
73
	ASSERT_NOT_REACHED;
74
	/* If the user can get here somehow, give a color that indicates a
75
	 * problem. */
76
	return &cairo_color_magenta;
77
    }
78
}
79

            
80
/* Convert a double in [0.0, 1.0] to an integer in [0, 65535]
81
 * The conversion is designed to choose the integer i such that
82
 * i / 65535.0 is as close as possible to the input value.
83
 */
84
uint16_t
85
6690452
_cairo_color_double_to_short (double d)
86
{
87
6690452
    return d * 65535.0 + 0.5;
88
}
89

            
90
static void
91
229958
_cairo_color_compute_shorts (cairo_color_t *color)
92
{
93
229958
    color->red_short   = _cairo_color_double_to_short (color->red   * color->alpha);
94
229958
    color->green_short = _cairo_color_double_to_short (color->green * color->alpha);
95
229958
    color->blue_short  = _cairo_color_double_to_short (color->blue  * color->alpha);
96
229958
    color->alpha_short = _cairo_color_double_to_short (color->alpha);
97
229958
}
98

            
99
void
100
229895
_cairo_color_init_rgba (cairo_color_t *color,
101
			double red, double green, double blue,
102
			double alpha)
103
{
104
229895
    color->red   = red;
105
229895
    color->green = green;
106
229895
    color->blue  = blue;
107
229895
    color->alpha = alpha;
108

            
109
229895
    _cairo_color_compute_shorts (color);
110
229895
}
111

            
112
void
113
63
_cairo_color_multiply_alpha (cairo_color_t *color,
114
			     double	    alpha)
115
{
116
63
    color->alpha *= alpha;
117

            
118
63
    _cairo_color_compute_shorts (color);
119
63
}
120

            
121
void
122
63
_cairo_color_get_rgba (cairo_color_t *color,
123
		       double	     *red,
124
		       double	     *green,
125
		       double	     *blue,
126
		       double	     *alpha)
127
{
128
63
    *red   = color->red;
129
63
    *green = color->green;
130
63
    *blue  = color->blue;
131
63
    *alpha = color->alpha;
132
63
}
133

            
134
void
135
_cairo_color_get_rgba_premultiplied (cairo_color_t *color,
136
				     double	   *red,
137
				     double	   *green,
138
				     double	   *blue,
139
				     double	   *alpha)
140
{
141
    *red   = color->red   * color->alpha;
142
    *green = color->green * color->alpha;
143
    *blue  = color->blue  * color->alpha;
144
    *alpha = color->alpha;
145
}
146

            
147
/* NB: This function works both for unmultiplied and premultiplied colors */
148
cairo_bool_t
149
116774
_cairo_color_equal (const cairo_color_t *color_a,
150
	            const cairo_color_t *color_b)
151
{
152
116774
    if (color_a == color_b)
153
	return TRUE;
154

            
155
116774
    if (color_a->alpha_short != color_b->alpha_short)
156
60144
        return FALSE;
157

            
158
56630
    if (color_a->alpha_short == 0)
159
        return TRUE;
160

            
161
72893
    return color_a->red_short   == color_b->red_short   &&
162
64004
           color_a->green_short == color_b->green_short &&
163
7374
           color_a->blue_short  == color_b->blue_short;
164
}
165

            
166
cairo_bool_t
167
894
_cairo_color_stop_equal (const cairo_color_stop_t *color_a,
168
			 const cairo_color_stop_t *color_b)
169
{
170
894
    if (color_a == color_b)
171
	return TRUE;
172

            
173
1221
    return color_a->alpha_short == color_b->alpha_short &&
174
327
           color_a->red_short   == color_b->red_short   &&
175
1260
           color_a->green_short == color_b->green_short &&
176
39
           color_a->blue_short  == color_b->blue_short;
177
}
178

            
179
cairo_content_t
180
_cairo_color_get_content (const cairo_color_t *color)
181
{
182
    if (CAIRO_COLOR_IS_OPAQUE (color))
183
        return CAIRO_CONTENT_COLOR;
184

            
185
    if (color->red_short == 0 &&
186
	color->green_short == 0 &&
187
	color->blue_short == 0)
188
    {
189
        return CAIRO_CONTENT_ALPHA;
190
    }
191

            
192
    return CAIRO_CONTENT_COLOR_ALPHA;
193
}