1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2012 Benjamin Otte <otte@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, see <http://www.gnu.org/licenses/>.
20 #include "gtkhslaprivate.h"
25 _gtk_hsla_init (GtkHSLA *hsla,
31 g_return_if_fail (hsla != NULL);
34 hsla->hue = fmod (hue, 360);
36 hsla->hue = fmod (hue, 360) + 360;
37 hsla->saturation = CLAMP (saturation, 0, 1);
38 hsla->lightness = CLAMP (lightness, 0, 1);
39 hsla->alpha = CLAMP (alpha, 0, 1);
43 _gtk_hsla_init_from_rgba (GtkHSLA *hsla,
53 g_return_if_fail (hsla != NULL);
54 g_return_if_fail (rgba != NULL);
85 hsla->lightness = (max + min) / 2;
88 hsla->alpha = rgba->alpha;
92 if (hsla->lightness <= 0.5)
93 hsla->saturation = (max - min) / (max + min);
95 hsla->saturation = (max - min) / (2 - max - min);
99 hsla->hue = (green - blue) / delta;
100 else if (green == max)
101 hsla->hue = 2 + (blue - red) / delta;
102 else if (blue == max)
103 hsla->hue = 4 + (red - green) / delta;
112 _gdk_rgba_init_from_hsla (GdkRGBA *rgba,
120 lightness = hsla->lightness;
121 saturation = hsla->saturation;
123 if (lightness <= 0.5)
124 m2 = lightness * (1 + saturation);
126 m2 = lightness + saturation - lightness * saturation;
127 m1 = 2 * lightness - m2;
129 rgba->alpha = hsla->alpha;
133 rgba->red = lightness;
134 rgba->green = lightness;
135 rgba->blue = lightness;
139 hue = hsla->hue + 120;
146 rgba->red = m1 + (m2 - m1) * hue / 60;
150 rgba->red = m1 + (m2 - m1) * (240 - hue) / 60;
161 rgba->green = m1 + (m2 - m1) * hue / 60;
165 rgba->green = m1 + (m2 - m1) * (240 - hue) / 60;
169 hue = hsla->hue - 120;
176 rgba->blue = m1 + (m2 - m1) * hue / 60;
180 rgba->blue = m1 + (m2 - m1) * (240 - hue) / 60;
187 _gtk_hsla_shade (GtkHSLA *dest,
191 g_return_if_fail (dest != NULL);
192 g_return_if_fail (src != NULL);
194 dest->hue = src->hue;
196 dest->lightness = src->lightness * factor;
197 dest->lightness = CLAMP (dest->lightness, 0.0, 1.0);
199 dest->saturation = src->saturation * factor;
200 dest->saturation = CLAMP (dest->saturation, 0.0, 1.0);
202 dest->alpha = src->alpha;