[geany/geany] 4f1b2a: Fix converting color to hex for insertion

Vasiliy Faronov git-noreply at xxxxx
Sat Jul 8 15:46:01 UTC 2017


Branch:      refs/heads/master
Author:      Vasiliy Faronov <vfaronov at gmail.com>
Committer:   Vasiliy Faronov <vfaronov at gmail.com>
Date:        Sat, 08 Jul 2017 15:46:01 UTC
Commit:      4f1b2a031377cd4104d3bc60c02c7f3ee89268dd
             https://github.com/geany/geany/commit/4f1b2a031377cd4104d3bc60c02c7f3ee89268dd

Log Message:
-----------
Fix converting color to hex for insertion

Fixes #1527.

In win32_show_color_dialog, utils_scale_round is not necessary at all
because Get{R,G,B}Value [1] already return 0..255, which we can immediately
render as hex.

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/dd144923.aspx


Modified Paths:
--------------
    src/utils.c
    src/win32.c

Modified: src/utils.c
14 lines changed, 9 insertions(+), 5 deletions(-)
===================================================================
@@ -451,10 +451,14 @@ const gchar *utils_path_skip_root(const gchar *path)
 }
 
 
+/* Convert a fractional @a val in the range [0, 1] to a whole value in the range [0, @a factor].
+ * In particular, this is used for converting a @c GdkColor to the "#RRGGBB" format in a way that
+ * agrees with GTK+, so the "#RRGGBB" in the color picker is the same "#RRGGBB" that is inserted
+ * into the document. See https://github.com/geany/geany/issues/1527
+ */
 gdouble utils_scale_round(gdouble val, gdouble factor)
 {
-	/*val = floor(val * factor + 0.5);*/
-	val = floor(val);
+	val = floor(val * factor + 0.5);
 	val = MAX(val, 0);
 	val = MIN(val, factor);
 
@@ -881,9 +885,9 @@ gchar *utils_get_hex_from_color(GdkColor *color)
 	g_return_val_if_fail(color != NULL, NULL);
 
 	return g_strdup_printf("#%02X%02X%02X",
-		(guint) (utils_scale_round(color->red / 256, 255)),
-		(guint) (utils_scale_round(color->green / 256, 255)),
-		(guint) (utils_scale_round(color->blue / 256, 255)));
+		(guint) (utils_scale_round(color->red / 65535.0, 255)),
+		(guint) (utils_scale_round(color->green / 65535.0, 255)),
+		(guint) (utils_scale_round(color->blue / 65535.0, 255)));
 }
 
 


Modified: src/win32.c
5 lines changed, 1 insertions(+), 4 deletions(-)
===================================================================
@@ -626,10 +626,7 @@ void win32_show_color_dialog(const gchar *colour)
 	{
 		rgb_current = cc.rgbResult;
 		g_snprintf(hex, 11, "#%02X%02X%02X",
-	      (guint) (utils_scale_round(GetRValue(rgb_current), 255)),
-	      (guint) (utils_scale_round(GetGValue(rgb_current), 255)),
-	      (guint) (utils_scale_round(GetBValue(rgb_current), 255)));
-
+			GetRValue(rgb_current), GetGValue(rgb_current), GetBValue(rgb_current));
 		editor_insert_color(doc->editor, hex);
 	}
 	g_free(hex);



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list