Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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. _cairo_stock_color (cairo_stock_t stock)
  62. {
  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;
  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. void
  81. _cairo_color_init (cairo_color_t *color)
  82. {
  83.     *color = cairo_color_white;
  84. }
  85.  
  86. void
  87. _cairo_color_init_rgb (cairo_color_t *color,
  88.                        double red, double green, double blue)
  89. {
  90.     _cairo_color_init_rgba (color, red, green, blue, 1.0);
  91. }
  92.  
  93. /* Convert a double in [0.0, 1.0] to an integer in [0, 65535]
  94.  * The conversion is designed to divide the input range into 65536
  95.  * equally-sized regions. This is achieved by multiplying by 65536 and
  96.  * then special-casing the result of an input value of 1.0 so that it
  97.  * maps to 65535 instead of 65536.
  98.  */
  99. uint16_t
  100. _cairo_color_double_to_short (double d)
  101. {
  102.     uint32_t i;
  103.     i = (uint32_t) (d * 65536);
  104.     i -= (i >> 16);
  105.     return i;
  106. }
  107.  
  108. static void
  109. _cairo_color_compute_shorts (cairo_color_t *color)
  110. {
  111.     color->red_short   = _cairo_color_double_to_short (color->red   * color->alpha);
  112.     color->green_short = _cairo_color_double_to_short (color->green * color->alpha);
  113.     color->blue_short  = _cairo_color_double_to_short (color->blue  * color->alpha);
  114.     color->alpha_short = _cairo_color_double_to_short (color->alpha);
  115. }
  116.  
  117. void
  118. _cairo_color_init_rgba (cairo_color_t *color,
  119.                         double red, double green, double blue,
  120.                         double alpha)
  121. {
  122.     color->red   = red;
  123.     color->green = green;
  124.     color->blue  = blue;
  125.     color->alpha = alpha;
  126.  
  127.     _cairo_color_compute_shorts (color);
  128. }
  129.  
  130. void
  131. _cairo_color_multiply_alpha (cairo_color_t *color,
  132.                              double         alpha)
  133. {
  134.     color->alpha *= alpha;
  135.  
  136.     _cairo_color_compute_shorts (color);
  137. }
  138.  
  139. void
  140. _cairo_color_get_rgba (cairo_color_t *color,
  141.                        double        *red,
  142.                        double        *green,
  143.                        double        *blue,
  144.                        double        *alpha)
  145. {
  146.     *red   = color->red;
  147.     *green = color->green;
  148.     *blue  = color->blue;
  149.     *alpha = color->alpha;
  150. }
  151.  
  152. void
  153. _cairo_color_get_rgba_premultiplied (cairo_color_t *color,
  154.                                      double        *red,
  155.                                      double        *green,
  156.                                      double        *blue,
  157.                                      double        *alpha)
  158. {
  159.     *red   = color->red   * color->alpha;
  160.     *green = color->green * color->alpha;
  161.     *blue  = color->blue  * color->alpha;
  162.     *alpha = color->alpha;
  163. }
  164.  
  165. /* NB: This function works both for unmultiplied and premultiplied colors */
  166. cairo_bool_t
  167. _cairo_color_equal (const cairo_color_t *color_a,
  168.                     const cairo_color_t *color_b)
  169. {
  170.     if (color_a == color_b)
  171.         return TRUE;
  172.  
  173.     if (color_a->alpha_short != color_b->alpha_short)
  174.         return FALSE;
  175.  
  176.     if (color_a->alpha_short == 0)
  177.         return TRUE;
  178.  
  179.     return 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;
  182. }
  183.  
  184. cairo_bool_t
  185. _cairo_color_stop_equal (const cairo_color_stop_t *color_a,
  186.                          const cairo_color_stop_t *color_b)
  187. {
  188.     if (color_a == color_b)
  189.         return TRUE;
  190.  
  191.     return color_a->alpha_short == color_b->alpha_short &&
  192.            color_a->red_short   == color_b->red_short   &&
  193.            color_a->green_short == color_b->green_short &&
  194.            color_a->blue_short  == color_b->blue_short;
  195. }
  196.  
  197. cairo_content_t
  198. _cairo_color_get_content (const cairo_color_t *color)
  199. {
  200.     if (CAIRO_COLOR_IS_OPAQUE (color))
  201.         return CAIRO_CONTENT_COLOR;
  202.  
  203.     if (color->red_short == 0 &&
  204.         color->green_short == 0 &&
  205.         color->blue_short == 0)
  206.     {
  207.         return CAIRO_CONTENT_ALPHA;
  208.     }
  209.  
  210.     return CAIRO_CONTENT_COLOR_ALPHA;
  211. }
  212.