summaryrefslogtreecommitdiff
path: root/gtk
diff options
context:
space:
mode:
authorPaul Pogonyshev <pogonyshev@gmx.net>2009-05-16 00:12:46 +0300
committerPaul Pogonyshev <pogonyshev@gmx.net>2009-05-16 00:12:46 +0300
commitee2dfca5879125346bc309f4f13c553e12fb7460 (patch)
tree88e1e36dc66e54568e04a41384e3487f338571bd /gtk
parent15b7265a00d405d6115f45ba0b1f26019ef1a52b (diff)
downloadpygtk-ee2dfca5879125346bc309f4f13c553e12fb7460.tar.gz
Add HSV support to gtk.gdk.Color objects
Add read-only float attributes for hue, saturation and value of a color. Add a function to create a color objects out of HSV components. Add unit tests and document new features. Part of bug 546019.
Diffstat (limited to 'gtk')
-rw-r--r--gtk/gdk-base-types.defs3
-rw-r--r--gtk/gdkcolor.override83
2 files changed, 86 insertions, 0 deletions
diff --git a/gtk/gdk-base-types.defs b/gtk/gdk-base-types.defs
index f6799bf2..5f395344 100644
--- a/gtk/gdk-base-types.defs
+++ b/gtk/gdk-base-types.defs
@@ -233,6 +233,9 @@
'("gfloat" "red_float")
'("gfloat" "green_float")
'("gfloat" "blue_float")
+ '("gfloat" "hue")
+ '("gfloat" "saturation")
+ '("gfloat" "value")
)
)
diff --git a/gtk/gdkcolor.override b/gtk/gdkcolor.override
index f46b8783..6ac88814 100644
--- a/gtk/gdkcolor.override
+++ b/gtk/gdkcolor.override
@@ -139,6 +139,44 @@ _wrap_gdk_color_new(PyGBoxed *self,
}
%%
+define color_from_hsv
+static PyObject *
+_wrap_color_from_hsv (PyObject *ignored, PyObject *args, PyObject*kwargs)
+{
+ static char *kwlist[] = { "hue", "saturation", "value", NULL };
+ gdouble hue, saturation, value;
+ gdouble red, green, blue;
+ GdkColor color;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ddd:gtk.gdk.color_from_hsv", kwlist,
+ &hue, &saturation, &value))
+ return NULL;
+
+ /* See documentation of the class for rationale. */
+
+ hue -= floor(hue);
+
+ if (saturation > 1.0)
+ saturation = 1.0;
+ else if (saturation < 0.0)
+ saturation = 0.0;
+
+ if (value > 1.0)
+ value = 1.0;
+ else if (value < 0.0)
+ value = 0.0;
+
+ gtk_hsv_to_rgb(hue, saturation, value,
+ &red, &green, &blue);
+
+ color.red = red * 65535.0;
+ color.green = green * 65535.0;
+ color.blue = blue * 65535.0;
+
+ return pyg_boxed_new(GDK_TYPE_COLOR, &color, TRUE, TRUE);
+}
+
+%%
override-attr GdkColor.red
static int
_wrap_gdk_color__set_red(PyObject *self, PyObject *value, void *closure)
@@ -251,6 +289,51 @@ _wrap_gdk_color__set_blue_float(PyObject *self, PyObject *value, void *closure)
}
}
%%
+override-attr GdkColor.hue
+
+static PyObject *
+_wrap_gdk_color__get_hue(PyObject *self, void *closure)
+{
+ GdkColor *color = pyg_boxed_get(self, GdkColor);
+ gdouble red = color->red / 65535.0;
+ gdouble green = color->green / 65535.0;
+ gdouble blue = color->blue / 65535.0;
+ gdouble hue;
+
+ gtk_rgb_to_hsv(red, green, blue, &hue, NULL, NULL);
+ return PyFloat_FromDouble(hue);
+}
+%%
+override-attr GdkColor.saturation
+
+static PyObject *
+_wrap_gdk_color__get_saturation(PyObject *self, void *closure)
+{
+ GdkColor *color = pyg_boxed_get(self, GdkColor);
+ gdouble red = color->red / 65535.0;
+ gdouble green = color->green / 65535.0;
+ gdouble blue = color->blue / 65535.0;
+ gdouble saturation;
+
+ gtk_rgb_to_hsv(red, green, blue, NULL, &saturation, NULL);
+ return PyFloat_FromDouble(saturation);
+}
+%%
+override-attr GdkColor.value
+
+static PyObject *
+_wrap_gdk_color__get_value(PyObject *self, void *closure)
+{
+ GdkColor *color = pyg_boxed_get(self, GdkColor);
+ gdouble red = color->red / 65535.0;
+ gdouble green = color->green / 65535.0;
+ gdouble blue = color->blue / 65535.0;
+ gdouble value;
+
+ gtk_rgb_to_hsv(red, green, blue, NULL, NULL, &value);
+ return PyFloat_FromDouble(value);
+}
+%%
override gdk_color_parse kwargs
static PyObject *
_wrap_gdk_color_parse(PyObject *self, PyObject *args, PyObject *kwargs)