[vte] widget: Use new colour class



commit 8e8f3d13d48e7ee8e767f87bd00580b4f50c2e1e
Author: Christian Persch <chpe gnome org>
Date:   Tue Nov 24 23:10:00 2015 +0100

    widget: Use new colour class

 src/vte.cc         |  364 +++++++++++++++++-----------------------------------
 src/vtegtk.cc      |   25 +++-
 src/vteinternal.hh |   24 ++--
 src/vteseq.cc      |   66 ++--------
 src/vtetypes.cc    |   77 +++++++++++
 src/vtetypes.hh    |   12 ++-
 6 files changed, 249 insertions(+), 319 deletions(-)
---
diff --git a/src/vte.cc b/src/vte.cc
index 144e7a0..0863ec4 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -2485,43 +2485,25 @@ VteTerminalPrivate::get_color(int entry) const
 
 /* Set up a palette entry with a more-or-less match for the requested color. */
 void
-VteTerminalPrivate::set_color_internal(int entry,
-                                       int source,
-                                       vte::color::rgb const* proposed)
+VteTerminalPrivate::set_color(int entry,
+                              int source,
+                              vte::color::rgb const& proposed)
 {
-        if (entry < 0 || entry >= VTE_PALETTE_SIZE)
-                return;
+        g_assert(entry >= 0 && entry < VTE_PALETTE_SIZE);
 
        VtePaletteColor *palette_color = &m_palette[entry];
 
-       /* Save the requested color. */
-       if (proposed != NULL) {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Set %s color[%d] to (%04x,%04x,%04x).\n",
-                               source == VTE_COLOR_SOURCE_ESCAPE ? "escape" : "API",
-                               entry, proposed->red, proposed->green, proposed->blue);
-
-               if (palette_color->sources[source].is_set &&
-                   palette_color->sources[source].color.red == proposed->red &&
-                   palette_color->sources[source].color.green == proposed->green &&
-                   palette_color->sources[source].color.blue == proposed->blue) {
-                       return;
-               }
-               palette_color->sources[source].is_set = TRUE;
-               palette_color->sources[source].color.red = proposed->red;
-               palette_color->sources[source].color.green = proposed->green;
-               palette_color->sources[source].color.blue = proposed->blue;
-       } else {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Reset %s color[%d].\n",
-                               source == VTE_COLOR_SOURCE_ESCAPE ? "escape" : "API",
-                               entry);
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color[%d] to (%04x,%04x,%04x).\n",
+                         source == VTE_COLOR_SOURCE_ESCAPE ? "escape" : "API",
+                         entry, proposed.red, proposed.green, proposed.blue);
 
-               if (!palette_color->sources[source].is_set) {
-                       return;
-               }
-               palette_color->sources[source].is_set = FALSE;
-       }
+        if (palette_color->sources[source].is_set &&
+            palette_color->sources[source].color == proposed) {
+                return;
+        }
+        palette_color->sources[source].is_set = TRUE;
+        palette_color->sources[source].color = proposed;
 
        /* If we're not realized yet, there's nothing else to do. */
        if (!gtk_widget_get_realized(m_widget))
@@ -2540,102 +2522,39 @@ VteTerminalPrivate::set_color_internal(int entry,
                invalidate_all();
 }
 
-static void
-vte_terminal_generate_bold(vte::color::rgb const* foreground,
-                          vte::color::rgb const* background,
-                          double factor,
-                           vte::color::rgb *bold /* (out) (callee allocates) */)
-{
-       double fy, fcb, fcr, by, bcb, bcr, r, g, b;
-       g_assert(foreground != NULL);
-       g_assert(background != NULL);
-       g_assert(bold != NULL);
-       fy =   0.2990 * foreground->red +
-              0.5870 * foreground->green +
-              0.1140 * foreground->blue;
-       fcb = -0.1687 * foreground->red +
-             -0.3313 * foreground->green +
-              0.5000 * foreground->blue;
-       fcr =  0.5000 * foreground->red +
-             -0.4187 * foreground->green +
-             -0.0813 * foreground->blue;
-       by =   0.2990 * background->red +
-              0.5870 * background->green +
-              0.1140 * background->blue;
-       bcb = -0.1687 * background->red +
-             -0.3313 * background->green +
-              0.5000 * background->blue;
-       bcr =  0.5000 * background->red +
-             -0.4187 * background->green +
-             -0.0813 * background->blue;
-       fy = (factor * fy) + ((1.0 - factor) * by);
-       fcb = (factor * fcb) + ((1.0 - factor) * bcb);
-       fcr = (factor * fcr) + ((1.0 - factor) * bcr);
-       r = fy + 1.402 * fcr;
-       g = fy + 0.34414 * fcb - 0.71414 * fcr;
-       b = fy + 1.722 * fcb;
-       _vte_debug_print(VTE_DEBUG_MISC,
-                       "Calculated bold (%d, %d, %d) = (%lf,%lf,%lf)",
-                       foreground->red, foreground->green, foreground->blue,
-                       r, g, b);
-       bold->red = CLAMP(r, 0, 0xffff);
-       bold->green = CLAMP(g, 0, 0xffff);
-       bold->blue = CLAMP(b, 0, 0xffff);
-       _vte_debug_print(VTE_DEBUG_MISC,
-                       "= (%04x,%04x,%04x).\n",
-                       bold->red, bold->green, bold->blue);
-}
-
-/*
- * _vte_terminal_set_color_bold:
- * @terminal: a #VteTerminal
- * @bold: the new bold color
- *
- * Sets the color used to draw bold text in the default foreground color.
- */
-static void
-_vte_terminal_set_color_bold(VteTerminal *terminal,
-                             vte::color::rgb const* bold)
+void
+VteTerminalPrivate::reset_color(int entry,
+                                int source)
 {
-       _vte_debug_print(VTE_DEBUG_MISC,
-                       "Set bold color to (%04x,%04x,%04x).\n",
-                       bold->red, bold->green, bold->blue);
-       terminal->pvt->set_color_internal(VTE_BOLD_FG, VTE_COLOR_SOURCE_API, bold);
-}
+        g_assert(entry >= 0 && entry < VTE_PALETTE_SIZE);
 
-/*
- * _vte_terminal_set_color_foreground:
- * @terminal: a #VteTerminal
- * @foreground: the new foreground color
- *
- * Sets the foreground color used to draw normal text
- */
-static void
-_vte_terminal_set_color_foreground(VteTerminal *terminal,
-                                   vte::color::rgb const* foreground)
-{
-       _vte_debug_print(VTE_DEBUG_MISC,
-                       "Set foreground color to (%04x,%04x,%04x).\n",
-                       foreground->red, foreground->green, foreground->blue);
-       terminal->pvt->set_color_internal(VTE_DEFAULT_FG, VTE_COLOR_SOURCE_API, foreground);
-}
+       VtePaletteColor *palette_color = &m_palette[entry];
 
-/*
- * _vte_terminal_set_color_background:
- * @terminal: a #VteTerminal
- * @background: the new background color
- *
- * Sets the background color for text which does not have a specific background
- * color assigned.  Only has effect when no background image is set.
- */
-static void
-_vte_terminal_set_color_background(VteTerminal *terminal,
-                                   vte::color::rgb const* background)
-{
-       _vte_debug_print(VTE_DEBUG_MISC,
-                       "Set background color to (%04x,%04x,%04x).\n",
-                       background->red, background->green, background->blue);
-       terminal->pvt->set_color_internal(VTE_DEFAULT_BG, VTE_COLOR_SOURCE_API, background);
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Reset %s color[%d].\n",
+                         source == VTE_COLOR_SOURCE_ESCAPE ? "escape" : "API",
+                         entry);
+
+        if (!palette_color->sources[source].is_set) {
+                return;
+        }
+        palette_color->sources[source].is_set = FALSE;
+
+       /* If we're not realized yet, there's nothing else to do. */
+       if (!gtk_widget_get_realized(m_widget))
+               return;
+
+       /* If we're setting the background color, set the background color
+        * on the widget as well. */
+       if (entry == VTE_DEFAULT_BG) {
+               widget_background_update();
+       }
+
+       /* and redraw */
+       if (entry == VTE_CURSOR_BG)
+               invalidate_cursor_once();
+       else
+               invalidate_all();
 }
 
 bool
@@ -2655,86 +2574,6 @@ VteTerminalPrivate::set_background_alpha(double alpha)
 }
 
 /*
- * _vte_terminal_set_color_cursor:
- * @terminal: a #VteTerminal
- * @cursor_background: (allow-none): the new color to use for the text cursor, or %NULL
- *
- * Sets the background color for text which is under the cursor.  If %NULL, text
- * under the cursor will be drawn with foreground and background colors
- * reversed.
- */
-static void
-_vte_terminal_set_color_cursor(VteTerminal *terminal,
-                               vte::color::rgb const* cursor_background)
-{
-       if (cursor_background != NULL) {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Set cursor color to (%04x,%04x,%04x).\n",
-                               cursor_background->red,
-                               cursor_background->green,
-                               cursor_background->blue);
-       } else {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Reset cursor color.\n");
-       }
-       terminal->pvt->set_color_internal(VTE_CURSOR_BG, VTE_COLOR_SOURCE_API, cursor_background);
-}
-
-/*
- * _vte_terminal_set_color_highlight:
- * @terminal: a #VteTerminal
- * @highlight_background: (allow-none): the new color to use for highlighted text, or %NULL
- *
- * Sets the background color for text which is highlighted.  If %NULL,
- * it is unset.  If neither highlight background nor highlight foreground are set,
- * highlighted text (which is usually highlighted because it is selected) will
- * be drawn with foreground and background colors reversed.
- */
-static void
-_vte_terminal_set_color_highlight(VteTerminal *terminal,
-                                  vte::color::rgb const* highlight_background)
-{
-       if (highlight_background != NULL) {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Set highlight background color to (%04x,%04x,%04x).\n",
-                               highlight_background->red,
-                               highlight_background->green,
-                               highlight_background->blue);
-       } else {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Reset highlight background color.\n");
-       }
-       terminal->pvt->set_color_internal(VTE_HIGHLIGHT_BG, VTE_COLOR_SOURCE_API, highlight_background);
-}
-
-/*
- * _vte_terminal_set_color_highlight_foreground:
- * @terminal: a #VteTerminal
- * @highlight_foreground: (allow-none): the new color to use for highlighted text, or %NULL
- *
- * Sets the foreground color for text which is highlighted.  If %NULL,
- * it is unset.  If neither highlight background nor highlight foreground are set,
- * highlighted text (which is usually highlighted because it is selected) will
- * be drawn with foreground and background colors reversed.
- */
-static void
-_vte_terminal_set_color_highlight_foreground(VteTerminal *terminal,
-                                             vte::color::rgb const* highlight_foreground)
-{
-       if (highlight_foreground != NULL) {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Set highlight foreground color to (%04x,%04x,%04x).\n",
-                               highlight_foreground->red,
-                               highlight_foreground->green,
-                               highlight_foreground->blue);
-       } else {
-               _vte_debug_print(VTE_DEBUG_MISC,
-                               "Reset highlight foreground color.\n");
-       }
-       terminal->pvt->set_color_internal(VTE_HIGHLIGHT_FG, VTE_COLOR_SOURCE_API, highlight_foreground);
-}
-
-/*
  * _vte_terminal_set_colors:
  * @terminal: a #VteTerminal
  * @foreground: (allow-none): the new foreground color, or %NULL
@@ -2825,10 +2664,9 @@ _vte_terminal_set_colors(VteTerminal *terminal,
                                }
                                break;
                        case VTE_BOLD_FG:
-                               vte_terminal_generate_bold(terminal->pvt->get_color(VTE_DEFAULT_FG),
-                                                          terminal->pvt->get_color(VTE_DEFAULT_BG),
-                                                          1.8,
-                                                          &color);
+                                color = vte::color::rgb(terminal->pvt->get_color(VTE_DEFAULT_FG),
+                                                        terminal->pvt->get_color(VTE_DEFAULT_BG),
+                                                        1.8);
                                break;
                        case VTE_HIGHLIGHT_BG:
                                unset = TRUE;
@@ -2847,7 +2685,10 @@ _vte_terminal_set_colors(VteTerminal *terminal,
                }
 
                /* Set up the color entry. */
-               terminal->pvt->set_color_internal(i, VTE_COLOR_SOURCE_API, unset ? NULL : &color);
+                if (unset)
+                        terminal->pvt->reset_color(i, VTE_COLOR_SOURCE_API);
+                else
+                        terminal->pvt->set_color(i, VTE_COLOR_SOURCE_API, color);
        }
 }
 
@@ -2873,20 +2714,23 @@ _pango_color_from_rgba(vte::color::rgb *color,
  * If @bold is %NULL then the default color is used.
  */
 void
-VteTerminalPrivate::set_color_bold(GdkRGBA const* bold)
+VteTerminalPrivate::set_color_bold(vte::color::rgb const& color)
 {
-       vte::color::rgb color;
-
-       if (bold == nullptr) {
-               vte_terminal_generate_bold(get_color(VTE_DEFAULT_FG),
-                                          get_color(VTE_DEFAULT_BG),
-                                          1.8,
-                                          &color);
-       } else {
-               _pango_color_from_rgba(&color, bold);
-       }
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color to (%04x,%04x,%04x).\n", "bold",
+                         color.red, color.green, color.blue);
+        set_color(VTE_BOLD_FG, VTE_COLOR_SOURCE_API, color);
+}
 
-       _vte_terminal_set_color_bold(m_terminal, &color);
+void
+VteTerminalPrivate::reset_color_bold()
+{
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Reset %s color.\n", "bold");
+        set_color(VTE_BOLD_FG, VTE_COLOR_SOURCE_API,
+                  vte::color::rgb(get_color(VTE_DEFAULT_FG),
+                                  get_color(VTE_DEFAULT_BG),
+                                  1.8));
 }
 
 /*
@@ -2896,13 +2740,12 @@ VteTerminalPrivate::set_color_bold(GdkRGBA const* bold)
  * Sets the foreground color used to draw normal text.
  */
 void
-VteTerminalPrivate::set_color_foreground(GdkRGBA const* foreground)
+VteTerminalPrivate::set_color_foreground(vte::color::rgb const& color)
 {
-        g_assert(foreground != nullptr);
-
-       vte::color::rgb color;
-       _vte_terminal_set_color_foreground(m_terminal,
-                                           _pango_color_from_rgba(&color, foreground));
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color to (%04x,%04x,%04x).\n", "foreground",
+                         color.red, color.green, color.blue);
+       set_color(VTE_DEFAULT_FG, VTE_COLOR_SOURCE_API, color);
 }
 
 /*
@@ -2914,14 +2757,12 @@ VteTerminalPrivate::set_color_foreground(GdkRGBA const* foreground)
  * the terminal is not transparent.
  */
 void
-VteTerminalPrivate::set_color_background(GdkRGBA const *background)
+VteTerminalPrivate::set_color_background(vte::color::rgb const& color)
 {
-        g_assert(background != nullptr);
-
-        vte::color::rgb color;
-       _vte_terminal_set_color_background(m_terminal,
-                                           _pango_color_from_rgba(&color, background));
-        set_background_alpha(background->alpha);
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color to (%04x,%04x,%04x).\n", "background",
+                         color.red, color.green, color.blue);
+       set_color(VTE_DEFAULT_BG, VTE_COLOR_SOURCE_API, color);
 }
 
 /*
@@ -2933,15 +2774,24 @@ VteTerminalPrivate::set_color_background(GdkRGBA const *background)
  * reversed.
  */
 void
-VteTerminalPrivate::set_color_cursor(GdkRGBA const* cursor_background)
+VteTerminalPrivate::set_color_cursor_background(vte::color::rgb const& color)
 {
-        vte::color::rgb color;
-       _vte_terminal_set_color_cursor(m_terminal,
-                                       _pango_color_from_rgba(&color, cursor_background));
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color to (%04x,%04x,%04x).\n", "cursor background",
+                         color.red, color.green, color.blue);
+       set_color(VTE_CURSOR_BG, VTE_COLOR_SOURCE_API, color);
+}
+
+void
+VteTerminalPrivate::reset_color_cursor_background()
+{
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Reset %s color.\n", "cursor background");
+        reset_color(VTE_CURSOR_BG, VTE_COLOR_SOURCE_API);
 }
 
 /*
- * VteTerminalPrivate::set_color_highlight:
+ * VteTerminalPrivate::set_color_highlight_background:
  * @highlight_background: (allow-none): the new color to use for highlighted text, or %NULL
  *
  * Sets the background color for text which is highlighted.  If %NULL,
@@ -2950,11 +2800,20 @@ VteTerminalPrivate::set_color_cursor(GdkRGBA const* cursor_background)
  * be drawn with foreground and background colors reversed.
  */
 void
-VteTerminalPrivate::set_color_highlight(GdkRGBA const *highlight_background)
+VteTerminalPrivate::set_color_highlight_background(vte::color::rgb const& color)
 {
-       vte::color::rgb color;
-       _vte_terminal_set_color_highlight(m_terminal,
-                                          _pango_color_from_rgba(&color, highlight_background));
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color to (%04x,%04x,%04x).\n", "highlight background",
+                         color.red, color.green, color.blue);
+       set_color(VTE_HIGHLIGHT_BG, VTE_COLOR_SOURCE_API, color);
+}
+
+void
+VteTerminalPrivate::reset_color_highlight_background()
+{
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Reset %s color.\n", "highlight background");
+        reset_color(VTE_HIGHLIGHT_BG, VTE_COLOR_SOURCE_API);
 }
 
 /*
@@ -2967,11 +2826,20 @@ VteTerminalPrivate::set_color_highlight(GdkRGBA const *highlight_background)
  * be drawn with foreground and background colors reversed.
  */
 void
-VteTerminalPrivate::set_color_highlight_foreground(GdkRGBA const *highlight_foreground)
+VteTerminalPrivate::set_color_highlight_foreground(vte::color::rgb const& color)
 {
-       vte::color::rgb color;
-       _vte_terminal_set_color_highlight_foreground(m_terminal,
-                                                     _pango_color_from_rgba(&color, highlight_foreground));
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Set %s color to (%04x,%04x,%04x).\n", "highlight foreground",
+                         color.red, color.green, color.blue);
+       set_color(VTE_HIGHLIGHT_FG, VTE_COLOR_SOURCE_API, color);
+}
+
+void
+VteTerminalPrivate::reset_color_highlight_foreground()
+{
+        _vte_debug_print(VTE_DEBUG_MISC,
+                         "Reset %s color.\n", "highlight foreground");
+        reset_color(VTE_HIGHLIGHT_FG, VTE_COLOR_SOURCE_API);
 }
 
 /*
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index bb78ed2..097778b 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -2599,7 +2599,8 @@ vte_terminal_set_color_background(VteTerminal *terminal,
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
         g_return_if_fail(background != NULL);
-        terminal->pvt->set_color_background(background);
+        terminal->pvt->set_color_background(vte::color::rgb(background));
+        terminal->pvt->set_background_alpha(background->alpha);
 }
 
 /**
@@ -2615,7 +2616,10 @@ vte_terminal_set_color_bold(VteTerminal *terminal,
                             const GdkRGBA *bold)
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
-        terminal->pvt->set_color_bold(bold);
+        if (bold)
+                terminal->pvt->set_color_bold(vte::color::rgb(bold));
+        else
+                terminal->pvt->reset_color_bold();
 }
 
 /**
@@ -2632,7 +2636,10 @@ vte_terminal_set_color_cursor(VteTerminal *terminal,
                               const GdkRGBA *cursor_background)
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
-        terminal->pvt->set_color_cursor(cursor_background);
+        if (cursor_background)
+                terminal->pvt->set_color_cursor_background(vte::color::rgb(cursor_background));
+        else
+                terminal->pvt->reset_color_cursor_background();
 }
 
 /**
@@ -2648,7 +2655,7 @@ vte_terminal_set_color_foreground(VteTerminal *terminal,
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
         g_return_if_fail(foreground != NULL);
-        terminal->pvt->set_color_foreground(foreground);
+        terminal->pvt->set_color_foreground(vte::color::rgb(foreground));
 }
 
 /**
@@ -2666,7 +2673,10 @@ vte_terminal_set_color_highlight(VteTerminal *terminal,
                                  const GdkRGBA *highlight_background)
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
-        terminal->pvt->set_color_highlight(highlight_background);
+        if (highlight_background)
+                terminal->pvt->set_color_highlight_background(vte::color::rgb(highlight_background));
+        else
+                terminal->pvt->reset_color_highlight_background();
 }
 
 /**
@@ -2684,7 +2694,10 @@ vte_terminal_set_color_highlight_foreground(VteTerminal *terminal,
                                             const GdkRGBA *highlight_foreground)
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
-        terminal->pvt->set_color_highlight_foreground(highlight_foreground);
+        if (highlight_foreground)
+                terminal->pvt->set_color_highlight_foreground(vte::color::rgb(highlight_foreground));
+        else
+                terminal->pvt->reset_color_highlight_foreground();
 }
 
 /**
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 66a9c43..fe6e450 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -737,21 +737,27 @@ public:
         long get_char_width()  { ensure_font(); return char_width;  }
 
         vte::color::rgb const* get_color(int entry) const;
-        void set_color_internal(int entry,
-                                int source,
-                                vte::color::rgb const* proposed);
+        void set_color(int entry,
+                       int source,
+                       vte::color::rgb const& proposed);
+        void reset_color(int entry,
+                         int source);
 
         bool set_audible_bell(bool setting);
         bool set_allow_bold(bool setting);
         bool set_backspace_binding(VteEraseBinding binding);
         bool set_background_alpha(double alpha);
         bool set_cjk_ambiguous_width(int width);
-        void set_color_background(GdkRGBA const *background);
-        void set_color_bold(GdkRGBA const* bold);
-        void set_color_cursor(GdkRGBA const* cursor_background);
-        void set_color_foreground(GdkRGBA const* foreground);
-        void set_color_highlight(GdkRGBA const *highlight_background);
-        void set_color_highlight_foreground(GdkRGBA const *highlight_foreground);
+        void set_color_background(vte::color::rgb const &color);
+        void set_color_bold(vte::color::rgb const& color);
+        void reset_color_bold();
+        void set_color_cursor_background(vte::color::rgb const& color);
+        void reset_color_cursor_background();
+        void set_color_foreground(vte::color::rgb const& color);
+        void set_color_highlight_background(vte::color::rgb const& color);
+        void reset_color_highlight_background();
+        void set_color_highlight_foreground(vte::color::rgb const& color);
+        void reset_color_highlight_foreground();
         void set_colors(GdkRGBA const *foreground,
                         GdkRGBA const *background,
                         GdkRGBA const *palette,
diff --git a/src/vteseq.cc b/src/vteseq.cc
index cdfc017..2f04308 100644
--- a/src/vteseq.cc
+++ b/src/vteseq.cc
@@ -114,50 +114,6 @@ vte_ucs4_to_utf8 (VteTerminal *terminal, const guchar *in)
        return out;
 }
 
-static gboolean
-vte_parse_color (const char *spec, vte::color::rgb *color)
-{
-       gchar *spec_copy = (gchar *) spec;
-       gboolean retval = FALSE;
-        GdkColor gdk_color;
-
-       /* gdk_color_parse doesnt handle all XParseColor formats.  It only
-        * supports the #RRRGGGBBB format, not the rgb:RRR/GGG/BBB format.
-        * See: man XParseColor */
-
-       if (g_ascii_strncasecmp (spec_copy, "rgb:", 4) == 0) {
-               gchar *cur, *ptr;
-
-               spec_copy = g_strdup (spec);
-               cur = spec_copy;
-               ptr = spec_copy + 3;
-
-               *cur++ = '#';
-               while (*ptr++)
-                       if (*ptr != '/')
-                               *cur++ = *ptr;
-               *cur++ = '\0';
-       }
-
-       retval = gdk_color_parse (spec_copy, &gdk_color);
-
-       if (spec_copy != spec)
-               g_free (spec_copy);
-
-        if (retval) {
-                color->red = gdk_color.red;
-                color->green = gdk_color.green;
-                color->blue = gdk_color.blue;
-        }
-
-       return retval;
-}
-
-
-
-
-
-
 /* Emit a "deiconify-window" signal. */
 static void
 vte_terminal_emit_deiconify_window(VteTerminal *terminal)
@@ -1652,8 +1608,8 @@ vte_sequence_handler_change_color_internal (VteTerminal *terminal, GValueArray *
                        if (idx >= VTE_DEFAULT_FG)
                                continue;
 
-                       if (vte_parse_color (pairs[i + 1], &color)) {
-                                terminal->pvt->set_color_internal(idx, VTE_COLOR_SOURCE_ESCAPE, &color);
+                       if (color.parse(pairs[i + 1])) {
+                                terminal->pvt->set_color(idx, VTE_COLOR_SOURCE_ESCAPE, color);
                        } else if (strcmp (pairs[i + 1], "?") == 0) {
                                gchar buf[128];
                                vte::color::rgb const* c = terminal->pvt->get_color(idx);
@@ -1706,11 +1662,11 @@ vte_sequence_handler_reset_color (VteTerminal *terminal, GValueArray *params)
                        if (idx < 0 || idx >= VTE_DEFAULT_FG)
                                continue;
 
-                       terminal->pvt->set_color_internal(idx, VTE_COLOR_SOURCE_ESCAPE, NULL);
+                       terminal->pvt->reset_color(idx, VTE_COLOR_SOURCE_ESCAPE);
                }
        } else {
                for (idx = 0; idx < VTE_DEFAULT_FG; idx++) {
-                       terminal->pvt->set_color_internal(idx, VTE_COLOR_SOURCE_ESCAPE, NULL);
+                       terminal->pvt->reset_color(idx, VTE_COLOR_SOURCE_ESCAPE);
                }
        }
 }
@@ -3125,8 +3081,8 @@ vte_sequence_handler_change_special_color_internal (VteTerminal *terminal, GValu
                if (! name)
                        return;
 
-               if (vte_parse_color (name, &color))
-                       terminal->pvt->set_color_internal(index, VTE_COLOR_SOURCE_ESCAPE, &color);
+               if (color.parse(name))
+                       terminal->pvt->set_color(index, VTE_COLOR_SOURCE_ESCAPE, color);
                else if (strcmp (name, "?") == 0) {
                        gchar buf[128];
                        vte::color::rgb const* c = terminal->pvt->get_color(index);
@@ -3163,7 +3119,7 @@ vte_sequence_handler_change_foreground_color_st (VteTerminal *terminal, GValueAr
 static void
 vte_sequence_handler_reset_foreground_color (VteTerminal *terminal, GValueArray *params)
 {
-       terminal->pvt->set_color_internal(VTE_DEFAULT_FG, VTE_COLOR_SOURCE_ESCAPE, NULL);
+       terminal->pvt->reset_color(VTE_DEFAULT_FG, VTE_COLOR_SOURCE_ESCAPE);
 }
 
 /* Change the default background cursor, BEL terminated */
@@ -3186,7 +3142,7 @@ vte_sequence_handler_change_background_color_st (VteTerminal *terminal, GValueAr
 static void
 vte_sequence_handler_reset_background_color (VteTerminal *terminal, GValueArray *params)
 {
-       terminal->pvt->set_color_internal(VTE_DEFAULT_BG, VTE_COLOR_SOURCE_ESCAPE, NULL);
+       terminal->pvt->reset_color(VTE_DEFAULT_BG, VTE_COLOR_SOURCE_ESCAPE);
 }
 
 /* Change the color of the cursor, BEL terminated */
@@ -3209,7 +3165,7 @@ vte_sequence_handler_change_cursor_color_st (VteTerminal *terminal, GValueArray
 static void
 vte_sequence_handler_reset_cursor_color (VteTerminal *terminal, GValueArray *params)
 {
-       terminal->pvt->set_color_internal(VTE_CURSOR_BG, VTE_COLOR_SOURCE_ESCAPE, NULL);
+       terminal->pvt->reset_color(VTE_CURSOR_BG, VTE_COLOR_SOURCE_ESCAPE);
 }
 
 /* Change the highlight background color, BEL terminated */
@@ -3232,7 +3188,7 @@ vte_sequence_handler_change_highlight_background_color_st (VteTerminal *terminal
 static void
 vte_sequence_handler_reset_highlight_background_color (VteTerminal *terminal, GValueArray *params)
 {
-       terminal->pvt->set_color_internal(VTE_HIGHLIGHT_BG, VTE_COLOR_SOURCE_ESCAPE, NULL);
+       terminal->pvt->reset_color(VTE_HIGHLIGHT_BG, VTE_COLOR_SOURCE_ESCAPE);
 }
 
 /* Change the highlight foreground color, BEL terminated */
@@ -3255,7 +3211,7 @@ vte_sequence_handler_change_highlight_foreground_color_st (VteTerminal *terminal
 static void
 vte_sequence_handler_reset_highlight_foreground_color (VteTerminal *terminal, GValueArray *params)
 {
-       terminal->pvt->set_color_internal(VTE_HIGHLIGHT_FG, VTE_COLOR_SOURCE_ESCAPE, NULL);
+       terminal->pvt->reset_color(VTE_HIGHLIGHT_FG, VTE_COLOR_SOURCE_ESCAPE);
 }
 
 
diff --git a/src/vtetypes.cc b/src/vtetypes.cc
index 9da365b..298e7d2 100644
--- a/src/vtetypes.cc
+++ b/src/vtetypes.cc
@@ -17,6 +17,8 @@
 
 #include "config.h"
 
+#include <glib.h>
+
 #include "vtetypes.hh"
 
 #include <type_traits>
@@ -30,6 +32,81 @@ static_assert(sizeof(vte::grid::span) == 4 * sizeof(long), "vte::grid::span size
 static_assert(std::is_pod<vte::color::rgb>::value, "vte::color::rgb not POD");
 static_assert(sizeof(vte::color::rgb) == sizeof(PangoColor), "vte::color::rgb size wrong");
 
+/* Colours mix */
+vte::color::rgb::rgb(vte::color::rgb const& foreground,
+                     vte::color::rgb const& background,
+                     double factor)
+{
+        double fy, fcb, fcr, by, bcb, bcr, r, g, b;
+       fy =   0.2990 * foreground.red +
+               0.5870 * foreground.green +
+               0.1140 * foreground.blue;
+        fcb = -0.1687 * foreground.red +
+              -0.3313 * foreground.green +
+               0.5000 * foreground.blue;
+        fcr =  0.5000 * foreground.red +
+              -0.4187 * foreground.green +
+              -0.0813 * foreground.blue;
+        by =   0.2990 * background.red +
+               0.5870 * background.green +
+               0.1140 * background.blue;
+        bcb = -0.1687 * background.red +
+              -0.3313 * background.green +
+               0.5000 * background.blue;
+        bcr =  0.5000 * background.red +
+              -0.4187 * background.green +
+              -0.0813 * background.blue;
+        fy = (factor * fy) + ((1.0 - factor) * by);
+        fcb = (factor * fcb) + ((1.0 - factor) * bcb);
+        fcr = (factor * fcr) + ((1.0 - factor) * bcr);
+        r = fy + 1.402 * fcr;
+        g = fy + 0.34414 * fcb - 0.71414 * fcr;
+        b = fy + 1.722 * fcb;
+        red = CLAMP(r, 0, 0xffff);
+        green = CLAMP(g, 0, 0xffff);
+        blue = CLAMP(b, 0, 0xffff);
+}
+
+vte::color::rgb::rgb(GdkRGBA const* rgba) {
+        g_assert(rgba);
+        /* FIXME: equal distribution! */
+        red   = rgba->red   * 65535.;
+        green = rgba->green * 65535.;
+        blue  = rgba->blue  * 65535.;
+}
+
+bool
+vte::color::rgb::parse(char const* spec)
+{
+       char *spec_copy = (char *)spec;
+       bool retval = false;
+
+       /* gdk_color_parse doesnt handle all XParseColor formats.  It only
+        * supports the #RRRGGGBBB format, not the rgb:RRR/GGG/BBB format.
+        * See: man XParseColor */
+
+       if (g_ascii_strncasecmp(spec_copy, "rgb:", 4) == 0) {
+               char *cur, *ptr;
+
+               spec_copy = g_strdup(spec);
+               cur = spec_copy;
+               ptr = spec_copy + 3;
+
+               *cur++ = '#';
+               while (*ptr++)
+                       if (*ptr != '/')
+                               *cur++ = *ptr;
+               *cur++ = '\0';
+       }
+
+       retval = pango_color_parse(this, spec_copy) != FALSE;
+
+       if (spec_copy != spec)
+               g_free(spec_copy);
+
+       return retval;
+}
+
 #ifdef MAIN
 
 #include <glib.h>
diff --git a/src/vtetypes.hh b/src/vtetypes.hh
index 8a0ba26..3b2a7c0 100644
--- a/src/vtetypes.hh
+++ b/src/vtetypes.hh
@@ -18,6 +18,7 @@
 #pragma once
 
 #include <pango/pango.h>
+#include <gdk/gdk.h>
 
 namespace vte {
 
@@ -94,9 +95,18 @@ namespace color {
         public:
                 rgb() = default;
                 rgb(PangoColor const& c) { *static_cast<PangoColor*>(this) = c; }
-                rgb(PangoColor const& a, PangoColor const& b, double f);
+                rgb(GdkRGBA const* c);
+
+                rgb(rgb const& a, rgb const& b, double f);
+                rgb(rgb const* a, rgb const* b, double f) : rgb(*a, *b, f) { }
+
+                bool parse(char const* spec);
 
                 void from_pango(PangoColor const& c) { *static_cast<PangoColor*>(this) = c; }
+
+                inline bool operator == (rgb const& rhs) const {
+                        return red == rhs.red && green == rhs.green && blue == rhs.blue;
+                }
         };
 
 } /* namespace color */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]