Back to index

lightning-sunbird  0.9+nobinonly
cairo-color.c
Go to the documentation of this file.
00001 /* cairo - a vector graphics library with display and print output
00002  *
00003  * Copyright © 2002 University of Southern California
00004  * Copyright © 2005 Red Hat, Inc.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it either under the terms of the GNU Lesser General Public
00008  * License version 2.1 as published by the Free Software Foundation
00009  * (the "LGPL") or, at your option, under the terms of the Mozilla
00010  * Public License Version 1.1 (the "MPL"). If you do not alter this
00011  * notice, a recipient may use your version of this file under either
00012  * the MPL or the LGPL.
00013  *
00014  * You should have received a copy of the LGPL along with this library
00015  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00017  * You should have received a copy of the MPL along with this library
00018  * in the file COPYING-MPL-1.1
00019  *
00020  * The contents of this file are subject to the Mozilla Public License
00021  * Version 1.1 (the "License"); you may not use this file except in
00022  * compliance with the License. You may obtain a copy of the License at
00023  * http://www.mozilla.org/MPL/
00024  *
00025  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
00026  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
00027  * the specific language governing rights and limitations.
00028  *
00029  * The Original Code is the cairo graphics library.
00030  *
00031  * The Initial Developer of the Original Code is University of Southern
00032  * California.
00033  *
00034  * Contributor(s):
00035  *     Carl D. Worth <cworth@cworth.org>
00036  */
00037 
00038 #include "cairoint.h"
00039 
00040 static cairo_color_t const cairo_color_white = {
00041     1.0,    1.0,    1.0,    1.0,
00042     0xffff, 0xffff, 0xffff, 0xffff
00043 };
00044 
00045 static cairo_color_t const cairo_color_black = {
00046     0.0, 0.0, 0.0, 1.0,
00047     0x0, 0x0, 0x0, 0xffff
00048 };
00049 
00050 static cairo_color_t const cairo_color_transparent = {
00051     0.0, 0.0, 0.0, 0.0,
00052     0x0, 0x0, 0x0, 0x0
00053 };
00054 
00055 static cairo_color_t const cairo_color_magenta = {
00056     1.0,    0.0, 1.0,    1.0,
00057     0xffff, 0x0, 0xffff, 0xffff
00058 };
00059 
00060 const cairo_color_t *
00061 _cairo_stock_color (cairo_stock_t stock)
00062 {
00063     switch (stock) {
00064     case CAIRO_STOCK_WHITE:
00065        return &cairo_color_white;
00066     case CAIRO_STOCK_BLACK:
00067        return &cairo_color_black;
00068     case CAIRO_STOCK_TRANSPARENT:
00069        return &cairo_color_transparent;
00070     }
00071 
00072     ASSERT_NOT_REACHED;
00073 
00074     /* If the user can get here somehow, give a color that indicates a
00075      * problem. */
00076     return &cairo_color_magenta;
00077 }
00078 
00079 void
00080 _cairo_color_init (cairo_color_t *color)
00081 {
00082     *color = cairo_color_white;
00083 }
00084 
00085 void
00086 _cairo_color_init_rgb (cairo_color_t *color,
00087                      double red, double green, double blue)
00088 {
00089     _cairo_color_init_rgba (color, red, green, blue, 1.0);
00090 }
00091 
00092 /* We multiply colors by (0x10000 - epsilon), such that we get a uniform
00093  * range even for 0xffff.  In other words, (1.0 - epsilon) would convert
00094  * to 0xffff, not 0xfffe.
00095  */
00096 #define CAIRO_COLOR_ONE_MINUS_EPSILON (65536.0 - 1e-5)
00097 
00098 static void
00099 _cairo_color_compute_shorts (cairo_color_t *color)
00100 {
00101     color->red_short   = color->red   * color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
00102     color->green_short = color->green * color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
00103     color->blue_short  = color->blue  * color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
00104     color->alpha_short = color->alpha * CAIRO_COLOR_ONE_MINUS_EPSILON;
00105 }
00106 
00107 void
00108 _cairo_color_init_rgba (cairo_color_t *color,
00109                      double red, double green, double blue,
00110                      double alpha)
00111 {
00112     color->red   = red;
00113     color->green = green;
00114     color->blue  = blue;
00115     color->alpha = alpha;
00116 
00117     _cairo_color_compute_shorts (color);
00118 }
00119 
00120 void
00121 _cairo_color_multiply_alpha (cairo_color_t *color,
00122                           double       alpha)
00123 {
00124     color->alpha *= alpha;
00125 
00126     _cairo_color_compute_shorts (color);
00127 }
00128 
00129 void
00130 _cairo_color_get_rgba (cairo_color_t *color,
00131                      double      *red,
00132                      double      *green,
00133                      double      *blue,
00134                      double      *alpha)
00135 {
00136     *red   = color->red;
00137     *green = color->green;
00138     *blue  = color->blue;
00139     *alpha = color->alpha;
00140 }
00141 
00142 void
00143 _cairo_color_get_rgba_premultiplied (cairo_color_t *color,
00144                                  double      *red,
00145                                  double      *green,
00146                                  double      *blue,
00147                                  double      *alpha)
00148 {
00149     *red   = color->red   * color->alpha;
00150     *green = color->green * color->alpha;
00151     *blue  = color->blue  * color->alpha;
00152     *alpha = color->alpha;
00153 }