From 896dd975d19a7df8790cbd82b7d53f13d63587e3 Mon Sep 17 00:00:00 2001 From: Damien Lespiau Date: Sun, 6 Jun 2010 22:57:02 +0100 Subject: [PATCH] cogl-color: Add cogl_color_unpremultiply() For some operations on pre-multiplied colors (say, replace the alpha value), you need to unpremultiply the color. This patch provides the counterpart to cogl_color_premultiply(). --- cogl/cogl-color.c | 11 +++++++++++ cogl/cogl-color.h | 13 +++++++++++++ doc/reference/cogl/cogl-sections.txt | 1 + 3 files changed, 25 insertions(+) diff --git a/cogl/cogl-color.c b/cogl/cogl-color.c index a89534e49..544d874fc 100644 --- a/cogl/cogl-color.c +++ b/cogl/cogl-color.c @@ -163,6 +163,17 @@ cogl_color_premultiply (CoglColor *color) color->blue = (color->blue * color->alpha + 128) / 255; } +void +cogl_color_unpremultiply (CoglColor *color) +{ + if (color->alpha != 0) + { + color->red = (color->red * 255) / color->alpha; + color->green = (color->green * 255) / color->alpha; + color->blue = (color->blue * 255) / color->alpha; + } +} + gboolean cogl_color_equal (gconstpointer v1, gconstpointer v2) { diff --git a/cogl/cogl-color.h b/cogl/cogl-color.h index 8f3458bbf..1e1a14da7 100644 --- a/cogl/cogl-color.h +++ b/cogl/cogl-color.h @@ -299,6 +299,19 @@ cogl_color_get_alpha (const CoglColor *color); void cogl_color_premultiply (CoglColor *color); +/** + * cogl_color_unpremultiply: + * @color: the color to unpremultiply + * + * Converts a pre-multiplied color to a non-premultiplied color. For + * example, semi-transparent red is (0.5, 0, 0, 0.5) when premultiplied + * and (1.0, 0, 0, 0.5) when non-premultiplied. + * + * Since: 1.4 + */ +void +cogl_color_unpremultiply (CoglColor *color); + /** * cogl_color_equal: * @v1: a #CoglColor diff --git a/doc/reference/cogl/cogl-sections.txt b/doc/reference/cogl/cogl-sections.txt index af4685d2d..f23a7b1ea 100644 --- a/doc/reference/cogl/cogl-sections.txt +++ b/doc/reference/cogl/cogl-sections.txt @@ -384,6 +384,7 @@ cogl_color_get_alpha_float cogl_color_premultiply +cogl_color_unpremultiply cogl_color_equal