1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtksymboliccolor.h"
22 #include "gtkstyleproperties.h"
25 G_DEFINE_BOXED_TYPE (GtkSymbolicColor, gtk_symbolic_color,
26 gtk_symbolic_color_ref, gtk_symbolic_color_unref)
27 G_DEFINE_BOXED_TYPE (GtkGradient, gtk_gradient,
28 gtk_gradient_ref, gtk_gradient_unref)
39 struct _GtkSymbolicColor
51 GtkSymbolicColor *color;
57 GtkSymbolicColor *color1;
58 GtkSymbolicColor *color2;
64 typedef struct ColorStop ColorStop;
69 GtkSymbolicColor *color;
87 * gtk_symbolic_color_new_literal:
90 * Creates a symbolic color pointing to a literal color.
92 * Returns: a newly created #GtkSymbolicColor
97 gtk_symbolic_color_new_literal (const GdkRGBA *color)
99 GtkSymbolicColor *symbolic_color;
101 g_return_val_if_fail (color != NULL, NULL);
103 symbolic_color = g_slice_new0 (GtkSymbolicColor);
104 symbolic_color->type = COLOR_TYPE_LITERAL;
105 symbolic_color->color = *color;
106 symbolic_color->ref_count = 1;
108 return symbolic_color;
112 * gtk_symbolic_color_new_name:
115 * Creates a symbolic color pointing to an unresolved named
116 * color. See gtk_style_context_lookup_color() and
117 * gtk_style_properties_lookup_color().
119 * Returns: a newly created #GtkSymbolicColor
124 gtk_symbolic_color_new_name (const gchar *name)
126 GtkSymbolicColor *symbolic_color;
128 g_return_val_if_fail (name != NULL, NULL);
130 symbolic_color = g_slice_new0 (GtkSymbolicColor);
131 symbolic_color->type = COLOR_TYPE_NAME;
132 symbolic_color->name = g_strdup (name);
133 symbolic_color->ref_count = 1;
135 return symbolic_color;
139 * gtk_symbolic_color_new_shade:
140 * @color: another #GtkSymbolicColor
141 * @factor: shading factor to apply to @color
143 * Creates a symbolic color defined as a shade of
144 * another color. A factor > 1.0 would resolve to
145 * a brighter color, while < 1.0 would resolve to
148 * Returns: A newly created #GtkSymbolicColor
153 gtk_symbolic_color_new_shade (GtkSymbolicColor *color,
156 GtkSymbolicColor *symbolic_color;
158 g_return_val_if_fail (color != NULL, NULL);
160 symbolic_color = g_slice_new0 (GtkSymbolicColor);
161 symbolic_color->type = COLOR_TYPE_SHADE;
162 symbolic_color->shade.color = gtk_symbolic_color_ref (color);
163 symbolic_color->shade.factor = factor;
164 symbolic_color->ref_count = 1;
166 return symbolic_color;
170 * gtk_symbolic_color_new_alpha:
171 * @color: another #GtkSymbolicColor
172 * @factor: factor to apply to @color alpha
174 * Creates a symbolic color by modifying the relative alpha
175 * value of @color. A factor < 1.0 would resolve to a more
176 * transparent color, while > 1.0 would resolve to a more
179 * Returns: A newly created #GtkSymbolicColor
184 gtk_symbolic_color_new_alpha (GtkSymbolicColor *color,
187 GtkSymbolicColor *symbolic_color;
189 g_return_val_if_fail (color != NULL, NULL);
191 symbolic_color = g_slice_new0 (GtkSymbolicColor);
192 symbolic_color->type = COLOR_TYPE_ALPHA;
193 symbolic_color->alpha.color = gtk_symbolic_color_ref (color);
194 symbolic_color->alpha.factor = factor;
195 symbolic_color->ref_count = 1;
197 return symbolic_color;
201 * gtk_symbolic_color_new_mix:
202 * @color1: color to mix
203 * @color2: another color to mix
204 * @factor: mix factor
206 * Creates a symbolic color defined as a mix of another
207 * two colors. a mix factor of 0 would resolve to @color1,
208 * while a factor of 1 would resolve to @color2.
210 * Returns: A newly created #GtkSymbolicColor
215 gtk_symbolic_color_new_mix (GtkSymbolicColor *color1,
216 GtkSymbolicColor *color2,
219 GtkSymbolicColor *symbolic_color;
221 g_return_val_if_fail (color1 != NULL, NULL);
222 g_return_val_if_fail (color1 != NULL, NULL);
224 symbolic_color = g_slice_new0 (GtkSymbolicColor);
225 symbolic_color->type = COLOR_TYPE_MIX;
226 symbolic_color->mix.color1 = gtk_symbolic_color_ref (color1);
227 symbolic_color->mix.color2 = gtk_symbolic_color_ref (color2);
228 symbolic_color->mix.factor = factor;
229 symbolic_color->ref_count = 1;
231 return symbolic_color;
235 * gtk_symbolic_color_ref:
236 * @color: a #GtkSymbolicColor
238 * Increases the reference count of @color
240 * Returns: the same @color
245 gtk_symbolic_color_ref (GtkSymbolicColor *color)
247 g_return_val_if_fail (color != NULL, NULL);
255 * gtk_symbolic_color_unref:
256 * @color: a #GtkSymbolicColor
258 * Decreases the reference count of @color, freeing its memory if the
259 * reference count reaches 0.
264 gtk_symbolic_color_unref (GtkSymbolicColor *color)
266 g_return_if_fail (color != NULL);
270 if (color->ref_count == 0)
274 case COLOR_TYPE_NAME:
275 g_free (color->name);
277 case COLOR_TYPE_SHADE:
278 gtk_symbolic_color_unref (color->shade.color);
280 case COLOR_TYPE_ALPHA:
281 gtk_symbolic_color_unref (color->alpha.color);
284 gtk_symbolic_color_unref (color->mix.color1);
285 gtk_symbolic_color_unref (color->mix.color2);
291 g_slice_free (GtkSymbolicColor, color);
296 rgb_to_hls (gdouble *r,
344 s = (max - min) / (max + min);
346 s = (max - min) / (2 - max - min);
350 h = (green - blue) / delta;
351 else if (green == max)
352 h = 2 + (blue - red) / delta;
353 else if (blue == max)
354 h = 4 + (red - green) / delta;
367 hls_to_rgb (gdouble *h,
380 if (lightness <= 0.5)
381 m2 = lightness * (1 + saturation);
383 m2 = lightness + saturation - lightness * saturation;
384 m1 = 2 * lightness - m2;
401 r = m1 + (m2 - m1) * hue / 60;
405 r = m1 + (m2 - m1) * (240 - hue) / 60;
416 g = m1 + (m2 - m1) * hue / 60;
420 g = m1 + (m2 - m1) * (240 - hue) / 60;
431 b = m1 + (m2 - m1) * hue / 60;
435 b = m1 + (m2 - m1) * (240 - hue) / 60;
446 _shade_color (GdkRGBA *color,
452 rgb_to_hls (&temp.red, &temp.green, &temp.blue);
454 temp.green *= factor;
455 if (temp.green > 1.0)
457 else if (temp.green < 0.0)
463 else if (temp.blue < 0.0)
466 hls_to_rgb (&temp.red, &temp.green, &temp.blue);
471 * gtk_symbolic_color_resolve:
472 * @color: a #GtkSymbolicColor
473 * @props: #GtkStyleProperties to use when resolving named colors
474 * @resolved_color: (out): return location for the resolved color
476 * If @color is resolvable, @resolved_color will be filled in
477 * with the resolved color, and %TRUE will be returned. Generally,
478 * if @color can't be resolved, it is due to it being defined on
479 * top of a named color that doesn't exist in @props.
481 * Returns: %TRUE if the color has been resolved
486 gtk_symbolic_color_resolve (GtkSymbolicColor *color,
487 GtkStyleProperties *props,
488 GdkRGBA *resolved_color)
490 g_return_val_if_fail (color != NULL, FALSE);
491 g_return_val_if_fail (resolved_color != NULL, FALSE);
495 case COLOR_TYPE_LITERAL:
496 *resolved_color = color->color;
498 case COLOR_TYPE_NAME:
500 GtkSymbolicColor *named_color;
502 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
504 named_color = gtk_style_properties_lookup_color (props, color->name);
509 return gtk_symbolic_color_resolve (named_color, props, resolved_color);
513 case COLOR_TYPE_SHADE:
517 if (!gtk_symbolic_color_resolve (color->shade.color, props, &shade))
520 _shade_color (&shade, color->shade.factor);
521 *resolved_color = shade;
527 case COLOR_TYPE_ALPHA:
531 if (!gtk_symbolic_color_resolve (color->alpha.color, props, &alpha))
534 *resolved_color = alpha;
535 resolved_color->alpha = CLAMP (alpha.alpha * color->alpha.factor, 0, 1);
541 GdkRGBA color1, color2;
543 if (!gtk_symbolic_color_resolve (color->mix.color1, props, &color1))
546 if (!gtk_symbolic_color_resolve (color->mix.color2, props, &color2))
549 resolved_color->red = CLAMP (color1.red + ((color2.red - color1.red) * color->mix.factor), 0, 1);
550 resolved_color->green = CLAMP (color1.green + ((color2.green - color1.green) * color->mix.factor), 0, 1);
551 resolved_color->blue = CLAMP (color1.blue + ((color2.blue - color1.blue) * color->mix.factor), 0, 1);
552 resolved_color->alpha = CLAMP (color1.alpha + ((color2.alpha - color1.alpha) * color->mix.factor), 0, 1);
559 g_assert_not_reached ();
567 * gtk_gradient_new_linear:
568 * @x0: X coordinate of the starting point
569 * @y0: Y coordinate of the starting point
570 * @x1: X coordinate of the end point
571 * @y1: Y coordinate of the end point
573 * Creates a new linear gradient along the line defined by (x0, y0) and (x1, y1). Before using the gradient
574 * a number of stop colors must be added through gtk_gradient_add_color_stop().
576 * Returns: A newly created #GtkGradient
581 gtk_gradient_new_linear (gdouble x0,
586 GtkGradient *gradient;
588 gradient = g_slice_new (GtkGradient);
589 gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
595 gradient->radius0 = 0;
596 gradient->radius1 = 0;
598 gradient->ref_count = 1;
604 * gtk_gradient_new_radial:
605 * @x0: X coordinate of the start circle
606 * @y0: Y coordinate of the start circle
607 * @radius0: radius of the start circle
608 * @x1: X coordinate of the end circle
609 * @y1: Y coordinate of the end circle
610 * @radius1: radius of the end circle
612 * Creates a new radial gradient along the two circles defined by (x0, y0, radius0) and
613 * (x1, y1, radius1). Before using the gradient a number of stop colors must be added
614 * through gtk_gradient_add_color_stop().
616 * Returns: A newly created #GtkGradient
621 gtk_gradient_new_radial (gdouble x0,
628 GtkGradient *gradient;
630 gradient = g_slice_new (GtkGradient);
631 gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
637 gradient->radius0 = radius0;
638 gradient->radius1 = radius1;
640 gradient->ref_count = 1;
646 * gtk_gradient_add_color_stop:
647 * @gradient: a #GtkGradient
648 * @offset: offset for the color stop
649 * @color: color to use
651 * Adds a stop color to @gradient.
656 gtk_gradient_add_color_stop (GtkGradient *gradient,
658 GtkSymbolicColor *color)
662 g_return_if_fail (gradient != NULL);
664 stop.offset = offset;
665 stop.color = gtk_symbolic_color_ref (color);
667 g_array_append_val (gradient->stops, stop);
672 * @gradient: a #GtkGradient
674 * Increases the reference count of @gradient.
676 * Returns: The same @gradient
681 gtk_gradient_ref (GtkGradient *gradient)
683 g_return_val_if_fail (gradient != NULL, NULL);
685 gradient->ref_count++;
691 * gtk_gradient_unref:
692 * @gradient: a #GtkGradient
694 * Decreases the reference count of @gradient, freeing its memory
695 * if the reference count reaches 0.
700 gtk_gradient_unref (GtkGradient *gradient)
702 g_return_if_fail (gradient != NULL);
704 gradient->ref_count--;
706 if (gradient->ref_count == 0)
710 for (i = 0; i < gradient->stops->len; i++)
714 stop = &g_array_index (gradient->stops, ColorStop, i);
715 gtk_symbolic_color_unref (stop->color);
718 g_array_free (gradient->stops, TRUE);
719 g_slice_free (GtkGradient, gradient);
724 * gtk_gradient_resolve:
725 * @gradient: a #GtkGradient
726 * @props: #GtkStyleProperties to use when resolving named colors
727 * @resolved_gradient: (out): return location for the resolved pattern
729 * If @gradient is resolvable, @resolved_gradient will be filled in
730 * with the resolved gradient as a cairo_pattern_t, and %TRUE will
731 * be returned. Generally, if @gradient can't be resolved, it is
732 * due to it being defined on top of a named color that doesn't
735 * Returns: %TRUE if the gradient has been resolved
740 gtk_gradient_resolve (GtkGradient *gradient,
741 GtkStyleProperties *props,
742 cairo_pattern_t **resolved_gradient)
744 cairo_pattern_t *pattern;
747 g_return_val_if_fail (gradient != NULL, FALSE);
748 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
749 g_return_val_if_fail (resolved_gradient != NULL, FALSE);
751 if (gradient->radius0 == 0 && gradient->radius1 == 0)
752 pattern = cairo_pattern_create_linear (gradient->x0, gradient->y0,
753 gradient->x1, gradient->y1);
755 pattern = cairo_pattern_create_radial (gradient->x0, gradient->y0,
757 gradient->x1, gradient->y1,
760 for (i = 0; i < gradient->stops->len; i++)
765 stop = &g_array_index (gradient->stops, ColorStop, i);
767 if (!gtk_symbolic_color_resolve (stop->color, props, &color))
769 cairo_pattern_destroy (pattern);
773 cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
774 color.red, color.green,
775 color.blue, color.alpha);
778 *resolved_gradient = pattern;