diff --git a/src/gui/TextEditor.cpp b/src/gui/TextEditor.cpp index a5b2b4ab..298f8aec 100644 --- a/src/gui/TextEditor.cpp +++ b/src/gui/TextEditor.cpp @@ -971,8 +971,9 @@ void TextEditor::paint(cairo_t* cr, GdkRectangle* repaintRect, double zoom) { bool hasSelection = gtk_text_buffer_get_selection_bounds(this->buffer, &start, &end); if (hasSelection) { + auto selectionColorU16 = Util::GdkRGBA_to_ColorU16(selectionColor); PangoAttribute* attrib = - pango_attr_background_new(selectionColor.red, selectionColor.green, selectionColor.blue); + pango_attr_background_new(selectionColorU16.red, selectionColorU16.green, selectionColorU16.blue); PangoAttrList* list = pango_layout_get_attributes(this->layout); attrib->start_index = getByteOffset(gtk_text_iter_get_offset(&start)); @@ -1010,8 +1011,7 @@ void TextEditor::paint(cairo_t* cr, GdkRectangle* repaintRect, double zoom) { // set the line always the same size on display cairo_set_line_width(cr, 1 / zoom); - cairo_set_source_rgb(cr, selectionColor.red / 65536.0, selectionColor.green / 65536.0, - selectionColor.blue / 65536.0); + gdk_cairo_set_source_rgba(cr, &selectionColor); cairo_rectangle(cr, x0 - 5 / zoom, y0 - 5 / zoom, width + 10 / zoom, height + 10 / zoom); cairo_stroke(cr); diff --git a/src/util/Color.h b/src/util/Color.h index 23023828..4d6b9e1a 100644 --- a/src/util/Color.h +++ b/src/util/Color.h @@ -17,7 +17,6 @@ #include - #ifndef XOURNAL_ENFORCE_COLOR using Color = uint32_t; #else @@ -84,6 +83,13 @@ struct hash { #endif +struct ColorU16 { + uint16_t red{}; + uint16_t green{}; + uint16_t blue{}; + uint16_t alpha{}; +}; + namespace Util { constexpr auto rgb_to_GdkRGBA(Color color) -> GdkRGBA; @@ -92,6 +98,8 @@ constexpr auto argb_to_GdkRGBA(Color color, double alpha) -> GdkRGBA; constexpr auto GdkRGBA_to_argb(const GdkRGBA& color) -> Color; constexpr auto GdkRGBA_to_rgb(const GdkRGBA& color) -> Color; +constexpr auto GdkRGBA_to_ColorU16(const GdkRGBA& color) -> ColorU16; + void cairo_set_source_rgbi(cairo_t* cr, Color color); void cairo_set_source_rgbi(cairo_t* cr, Color color, double alpha); @@ -137,3 +145,16 @@ constexpr auto Util::floatToUIntColor(const double color) -> uint32_t { // static_assert(MAX_COLOR < 256.0, "MAX_COLOR isn't smaller than 256"); return static_cast(color * MAX_COLOR); } + +constexpr auto Util::GdkRGBA_to_ColorU16(const GdkRGBA& color) -> ColorU16 { + auto floatToColorU16 = [](double color) { + constexpr double MAX_COLOR = 65536.0 - std::numeric_limits::epsilon() * (65536.0 / 2.0); + static_assert(MAX_COLOR < 65536.0, "MAX_COLOR isn't smaller than 65536"); + return static_cast(color * MAX_COLOR); + }; + + return {floatToColorU16(color.red), // + floatToColorU16(color.green), // + floatToColorU16(color.blue), // + floatToColorU16(color.alpha)}; +}