Blame gdk/gdkcolor.c

Packit 98cdb6
/* GDK - The GIMP Drawing Kit
Packit 98cdb6
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
Packit 98cdb6
 * file for a list of people on the GTK+ Team.  See the ChangeLog
Packit 98cdb6
 * files for a list of changes.  These files are distributed with
Packit 98cdb6
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
#include <time.h>
Packit 98cdb6
Packit 98cdb6
#include "gdkscreen.h"
Packit 98cdb6
#include "gdkcolor.h"
Packit 98cdb6
#include "gdkinternals.h"
Packit 98cdb6
#include "gdkalias.h"
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_colormap_ref:
Packit 98cdb6
 * @cmap: a #GdkColormap
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated function; use g_object_ref() instead.
Packit 98cdb6
 *
Packit 98cdb6
 * Return value: the colormap
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated: 2.0: Use g_object_ref() instead.
Packit 98cdb6
 **/
Packit 98cdb6
GdkColormap*
Packit 98cdb6
gdk_colormap_ref (GdkColormap *cmap)
Packit 98cdb6
{
Packit 98cdb6
  return (GdkColormap *) g_object_ref (cmap);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_colormap_unref:
Packit 98cdb6
 * @cmap: a #GdkColormap
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated function; use g_object_unref() instead.
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated: 2.0: Use g_object_unref() instead.
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_colormap_unref (GdkColormap *cmap)
Packit 98cdb6
{
Packit 98cdb6
  g_object_unref (cmap);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_colormap_get_visual:
Packit 98cdb6
 * @colormap: a #GdkColormap.
Packit 98cdb6
 * 
Packit 98cdb6
 * Returns the visual for which a given colormap was created.
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: the visual of the colormap.
Packit 98cdb6
 **/
Packit 98cdb6
GdkVisual *
Packit 98cdb6
gdk_colormap_get_visual (GdkColormap *colormap)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
Packit 98cdb6
Packit 98cdb6
  return colormap->visual;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_colors_store:
Packit 98cdb6
 * @colormap: a #GdkColormap.
Packit 98cdb6
 * @colors: the new color values.
Packit 98cdb6
 * @ncolors: the number of colors to change.
Packit 98cdb6
 * 
Packit 98cdb6
 * Changes the value of the first @ncolors colors in
Packit 98cdb6
 * a private colormap. This function is obsolete and
Packit 98cdb6
 * should not be used. See gdk_color_change().
Packit 98cdb6
 **/     
Packit 98cdb6
void
Packit 98cdb6
gdk_colors_store (GdkColormap   *colormap,
Packit 98cdb6
		  GdkColor      *colors,
Packit 98cdb6
		  gint           ncolors)
Packit 98cdb6
{
Packit 98cdb6
  gint i;
Packit 98cdb6
Packit 98cdb6
  for (i = 0; i < ncolors; i++)
Packit 98cdb6
    {
Packit 98cdb6
      colormap->colors[i].pixel = colors[i].pixel;
Packit 98cdb6
      colormap->colors[i].red = colors[i].red;
Packit 98cdb6
      colormap->colors[i].green = colors[i].green;
Packit 98cdb6
      colormap->colors[i].blue = colors[i].blue;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  gdk_colormap_change (colormap, ncolors);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_copy:
Packit 98cdb6
 * @color: a #GdkColor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Makes a copy of a color structure. The result
Packit 98cdb6
 * must be freed using gdk_color_free().
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: a copy of @color.
Packit 98cdb6
 **/
Packit 98cdb6
GdkColor*
Packit 98cdb6
gdk_color_copy (const GdkColor *color)
Packit 98cdb6
{
Packit 98cdb6
  GdkColor *new_color;
Packit 98cdb6
  
Packit 98cdb6
  g_return_val_if_fail (color != NULL, NULL);
Packit 98cdb6
Packit 98cdb6
  new_color = g_slice_new (GdkColor);
Packit 98cdb6
  *new_color = *color;
Packit 98cdb6
  return new_color;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_free:
Packit 98cdb6
 * @color: a #GdkColor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Frees a color structure created with 
Packit 98cdb6
 * gdk_color_copy().
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_color_free (GdkColor *color)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (color != NULL);
Packit 98cdb6
Packit 98cdb6
  g_slice_free (GdkColor, color);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_white:
Packit 98cdb6
 * @colormap: a #GdkColormap.
Packit 98cdb6
 * @color: the location to store the color.
Packit 98cdb6
 * 
Packit 98cdb6
 * Returns the white color for a given colormap. The resulting
Packit 98cdb6
 * value has already allocated been allocated. 
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: %TRUE if the allocation succeeded.
Packit 98cdb6
 **/
Packit 98cdb6
gboolean
Packit 98cdb6
gdk_color_white (GdkColormap *colormap,
Packit 98cdb6
		 GdkColor    *color)
Packit 98cdb6
{
Packit 98cdb6
  gint return_val;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (colormap != NULL, FALSE);
Packit 98cdb6
Packit 98cdb6
  if (color)
Packit 98cdb6
    {
Packit 98cdb6
      color->red = 65535;
Packit 98cdb6
      color->green = 65535;
Packit 98cdb6
      color->blue = 65535;
Packit 98cdb6
Packit 98cdb6
      return_val = gdk_colormap_alloc_color (colormap, color, FALSE, TRUE);
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    return_val = FALSE;
Packit 98cdb6
Packit 98cdb6
  return return_val;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_black:
Packit 98cdb6
 * @colormap: a #GdkColormap.
Packit 98cdb6
 * @color: the location to store the color.
Packit 98cdb6
 * 
Packit 98cdb6
 * Returns the black color for a given colormap. The resulting
Packit 98cdb6
 * value has already been allocated. 
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: %TRUE if the allocation succeeded.
Packit 98cdb6
 **/
Packit 98cdb6
gboolean
Packit 98cdb6
gdk_color_black (GdkColormap *colormap,
Packit 98cdb6
		 GdkColor    *color)
Packit 98cdb6
{
Packit 98cdb6
  gint return_val;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (colormap != NULL, FALSE);
Packit 98cdb6
Packit 98cdb6
  if (color)
Packit 98cdb6
    {
Packit 98cdb6
      color->red = 0;
Packit 98cdb6
      color->green = 0;
Packit 98cdb6
      color->blue = 0;
Packit 98cdb6
Packit 98cdb6
      return_val = gdk_colormap_alloc_color (colormap, color, FALSE, TRUE);
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    return_val = FALSE;
Packit 98cdb6
Packit 98cdb6
  return return_val;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/********************
Packit 98cdb6
 * Color allocation *
Packit 98cdb6
 ********************/
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_colormap_alloc_color:
Packit 98cdb6
 * @colormap: a #GdkColormap.
Packit 98cdb6
 * @color: the color to allocate. On return the
Packit 98cdb6
 *    <structfield>pixel</structfield> field will be
Packit 98cdb6
 *    filled in if allocation succeeds.
Packit 98cdb6
 * @writeable: If %TRUE, the color is allocated writeable
Packit 98cdb6
 *    (their values can later be changed using gdk_color_change()).
Packit 98cdb6
 *    Writeable colors cannot be shared between applications.
Packit 98cdb6
 * @best_match: If %TRUE, GDK will attempt to do matching against
Packit 98cdb6
 *    existing colors if the color cannot be allocated as requested.
Packit 98cdb6
 *
Packit 98cdb6
 * Allocates a single color from a colormap.
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: %TRUE if the allocation succeeded.
Packit 98cdb6
 **/
Packit 98cdb6
gboolean
Packit 98cdb6
gdk_colormap_alloc_color (GdkColormap *colormap,
Packit 98cdb6
			  GdkColor    *color,
Packit 98cdb6
			  gboolean     writeable,
Packit 98cdb6
			  gboolean     best_match)
Packit 98cdb6
{
Packit 98cdb6
  gboolean success;
Packit 98cdb6
Packit 98cdb6
  gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
Packit 98cdb6
			     &success);
Packit 98cdb6
Packit 98cdb6
  return success;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_alloc:
Packit 98cdb6
 * @colormap: a #GdkColormap.
Packit 98cdb6
 * @color: The color to allocate. On return, the 
Packit 98cdb6
 *    <structfield>pixel</structfield> field will be filled in.
Packit 98cdb6
 * 
Packit 98cdb6
 * Allocates a single color from a colormap.
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: %TRUE if the allocation succeeded.
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated: 2.2: Use gdk_colormap_alloc_color() instead.
Packit 98cdb6
 **/
Packit 98cdb6
gboolean
Packit 98cdb6
gdk_color_alloc (GdkColormap *colormap,
Packit 98cdb6
		 GdkColor    *color)
Packit 98cdb6
{
Packit 98cdb6
  gboolean success;
Packit 98cdb6
Packit 98cdb6
  gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
Packit 98cdb6
Packit 98cdb6
  return success;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_hash:
Packit 98cdb6
 * @colora: a #GdkColor.
Packit 98cdb6
 * 
Packit 98cdb6
 * A hash function suitable for using for a hash
Packit 98cdb6
 * table that stores #GdkColor's.
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: The hash function applied to @colora
Packit 98cdb6
 **/
Packit 98cdb6
guint
Packit 98cdb6
gdk_color_hash (const GdkColor *colora)
Packit 98cdb6
{
Packit 98cdb6
  return ((colora->red) +
Packit 98cdb6
	  (colora->green << 11) +
Packit 98cdb6
	  (colora->blue << 22) +
Packit 98cdb6
	  (colora->blue >> 6));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_equal:
Packit 98cdb6
 * @colora: a #GdkColor.
Packit 98cdb6
 * @colorb: another #GdkColor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Compares two colors. 
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: %TRUE if the two colors compare equal
Packit 98cdb6
 **/
Packit 98cdb6
gboolean
Packit 98cdb6
gdk_color_equal (const GdkColor *colora,
Packit 98cdb6
		 const GdkColor *colorb)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (colora != NULL, FALSE);
Packit 98cdb6
  g_return_val_if_fail (colorb != NULL, FALSE);
Packit 98cdb6
Packit 98cdb6
  return ((colora->red == colorb->red) &&
Packit 98cdb6
	  (colora->green == colorb->green) &&
Packit 98cdb6
	  (colora->blue == colorb->blue));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
GType
Packit 98cdb6
gdk_color_get_type (void)
Packit 98cdb6
{
Packit 98cdb6
  static GType our_type = 0;
Packit 98cdb6
  
Packit 98cdb6
  if (our_type == 0)
Packit 98cdb6
    our_type = g_boxed_type_register_static (g_intern_static_string ("GdkColor"),
Packit 98cdb6
					     (GBoxedCopyFunc)gdk_color_copy,
Packit 98cdb6
					     (GBoxedFreeFunc)gdk_color_free);
Packit 98cdb6
  return our_type;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_parse:
Packit 98cdb6
 * @spec: the string specifying the color.
Packit 98cdb6
 * @color: (out): the #GdkColor to fill in
Packit 98cdb6
 *
Packit 98cdb6
 * Parses a textual specification of a color and fill in the
Packit 98cdb6
 * <structfield>red</structfield>, <structfield>green</structfield>,
Packit 98cdb6
 * and <structfield>blue</structfield> fields of a #GdkColor
Packit 98cdb6
 * structure. The color is <emphasis>not</emphasis> allocated, you
Packit 98cdb6
 * must call gdk_colormap_alloc_color() yourself. The string can
Packit 98cdb6
 * either one of a large set of standard names. (Taken from the X11
Packit 98cdb6
 * <filename>rgb.txt</filename> file), or it can be a hex value in the
Packit 98cdb6
 * form '#rgb' '#rrggbb' '#rrrgggbbb' or
Packit 98cdb6
 * '#rrrrggggbbbb' where 'r', 'g' and 'b' are hex digits of the
Packit 98cdb6
 * red, green, and blue components of the color, respectively. (White
Packit 98cdb6
 * in the four forms is '#fff' '#ffffff' '#fffffffff' and
Packit 98cdb6
 * '#ffffffffffff')
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: %TRUE if the parsing succeeded.
Packit 98cdb6
 **/
Packit 98cdb6
gboolean
Packit 98cdb6
gdk_color_parse (const gchar *spec,
Packit 98cdb6
		 GdkColor    *color)
Packit 98cdb6
{
Packit 98cdb6
  PangoColor pango_color;
Packit 98cdb6
Packit 98cdb6
  if (pango_color_parse (&pango_color, spec))
Packit 98cdb6
    {
Packit 98cdb6
      color->red = pango_color.red;
Packit 98cdb6
      color->green = pango_color.green;
Packit 98cdb6
      color->blue = pango_color.blue;
Packit 98cdb6
Packit 98cdb6
      return TRUE;
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    return FALSE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_color_to_string:
Packit 98cdb6
 * @color: a #GdkColor
Packit 98cdb6
 *
Packit 98cdb6
 * Returns a textual specification of @color in the hexadecimal form
Packit 98cdb6
 * <literal>#rrrrggggbbbb</literal>, where <literal>r</literal>,
Packit 98cdb6
 * <literal>g</literal> and <literal>b</literal> are hex digits
Packit 98cdb6
 * representing the red, green and blue components respectively.
Packit 98cdb6
 *
Packit 98cdb6
 * Return value: a newly-allocated text string
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.12
Packit 98cdb6
 **/
Packit 98cdb6
gchar *
Packit 98cdb6
gdk_color_to_string (const GdkColor *color)
Packit 98cdb6
{
Packit 98cdb6
  PangoColor pango_color;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (color != NULL, NULL);
Packit 98cdb6
Packit 98cdb6
  pango_color.red = color->red;
Packit 98cdb6
  pango_color.green = color->green;
Packit 98cdb6
  pango_color.blue = color->blue;
Packit 98cdb6
Packit 98cdb6
  return pango_color_to_string (&pango_color);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_colormap_get_system:
Packit 98cdb6
 * 
Packit 98cdb6
 * Gets the system's default colormap for the default screen. (See
Packit 98cdb6
 * gdk_colormap_get_system_for_screen ())
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: the default colormap.
Packit 98cdb6
 **/
Packit 98cdb6
GdkColormap*
Packit 98cdb6
gdk_colormap_get_system (void)
Packit 98cdb6
{
Packit 98cdb6
  return gdk_screen_get_system_colormap (gdk_screen_get_default ());
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#define __GDK_COLOR_C__
Packit 98cdb6
#include "gdkaliasdef.c"