diff --git a/development/FORMAT b/development/FORMAT index 4e6dcc0f1c..2fff885219 100644 --- a/development/FORMAT +++ b/development/FORMAT @@ -7,6 +7,10 @@ changes happened in particular if possible. A good example would be ----------------------- +2021-01-19 Jürgen Spitzmüller + * Format incremented to 604: Branch colors now take two values: + \color lightmode darkmode + 2021-01-18 Jürgen Spitzmüller * Format incremented to 603: New InsetGraphics param darkModeSensitive This advises LyX to revert colors in dark mode. diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py index 0fc850baef..d3177f9787 100644 --- a/lib/lyx2lyx/lyx_2_4.py +++ b/lib/lyx2lyx/lyx_2_4.py @@ -4086,6 +4086,26 @@ def revert_darkmode_graphics(document): i += 1 +def revert_branch_darkcols(document): + " Revert dark branch colors " + + i = 0 + while True: + i = find_token(document.header, "\\branch", i) + if i == -1: + break + j = find_token(document.header, "\\end_branch", i) + if j == -1: + document.warning("Malformed LyX document. Can't find end of branch definition!") + break + k = find_token(document.header, "\\color", i, j) + if k != -1: + m = re.search('\\\\color (\S+) (\S+)', document.header[k]) + if m: + document.header[k] = "\\color " + m.group(1) + i += 1 + + ## # Conversion hub # @@ -4150,10 +4170,12 @@ convert = [ [600, []], [601, [convert_math_refs]], [602, [convert_branch_colors]], - [603, []] + [603, []], + [604, []] ] -revert = [[602, [revert_darkmode_graphics]], +revert = [[603, [revert_branch_darkcols]], + [602, [revert_darkmode_graphics]], [601, [revert_branch_colors]], [600, []], [599, [revert_math_refs]], diff --git a/src/BranchList.cpp b/src/BranchList.cpp index aa8551ed5d..f21fec57a7 100644 --- a/src/BranchList.cpp +++ b/src/BranchList.cpp @@ -29,7 +29,8 @@ namespace lyx { Branch::Branch() : selected_(false), filenameSuffix_(false) { - color_ = "background"; + lmcolor_ = "background"; + dmcolor_ = "background"; } @@ -74,23 +75,57 @@ void Branch::setFileNameSuffix(bool b) string const & Branch::color() const { - return color_; + return (theApp() && theApp()->isInDarkMode()) + ? dmcolor_ : lmcolor_; } -void Branch::setColor(string const & str) +string const & Branch::lightModeColor() const { - color_ = str; + return lmcolor_; +} + + +string const & Branch::darkModeColor() const +{ + return dmcolor_; +} + + +void Branch::setColor(string const & col) +{ + if (theApp() && theApp()->isInDarkMode()) + setColors(string(), col); + else + setColors(col); +} + + +void Branch::setColors(string const & lmcol, string const & dmcol) +{ + if (lmcol.empty() && lmcolor_ == "background" && support::prefixIs(dmcol, "#")) + lmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(dmcol))); + else if (!lmcol.empty()) + lmcolor_ = lmcol; + if (dmcol.empty() && dmcolor_ == "background" && support::prefixIs(lmcol, "#")) + dmcolor_ = X11hexname(inverseRGBColor(rgbFromHexName(lmcol))); + else if (!dmcol.empty()) + dmcolor_ = dmcol; // Update the Color table - string color = str; - bool darkmode = theApp() ? theApp()->isInDarkMode() : false; - if (color == "none") - color = lcolor.getX11HexName(Color_background, darkmode); - else if (color.size() != 7 || color[0] != '#') - color = lcolor.getX11HexName(lcolor.getFromLyXName(color), darkmode); + string lmcolor = lmcolor_; + string dmcolor = dmcolor_; + if (lmcolor == "none") + lmcolor = lcolor.getX11HexName(Color_background); + else if (lmcolor.size() != 7 || lmcolor[0] != '#') + lmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(lmcolor)); + if (dmcolor == "none") + lmcolor = lcolor.getX11HexName(Color_background, true); + else if (dmcolor.size() != 7 || dmcolor[0] != '#') + dmcolor = lcolor.getX11HexName(lcolor.getFromLyXName(dmcolor), true); + // FIXME UNICODE - lcolor.setColor(to_utf8(branch_), color); + lcolor.setColor(to_utf8(branch_), lmcolor, dmcolor); } diff --git a/src/BranchList.h b/src/BranchList.h index cdae7b6d0c..2c6d0eb578 100644 --- a/src/BranchList.h +++ b/src/BranchList.h @@ -62,13 +62,21 @@ public: void setFileNameSuffix(bool); /// std::string const & color() const; + /// + std::string const & lightModeColor() const; + /// + std::string const & darkModeColor() const; /** - * Set color from a hexcolor string "#rrggbb" or a lyx color name. + * Set background color from a hexcolor string "#rrggbb" or a lyx color name. * Use Color:background if the string is no valid color. * This ensures compatibility with LyX 1.4.0 that had the symbolic * color "none" that was displayed as Color:background. + * This sets the dark color if in dark mode, else the light color. */ - void setColor(std::string const &); + void setColor(std::string const & color); + /// Set dark and light background colors + void setColors(std::string const & color, + std::string const & dmcolor = std::string()); private: /// @@ -77,8 +85,10 @@ private: bool selected_; /// bool filenameSuffix_; - /// - std::string color_; + /// light mode background color + std::string lmcolor_; + /// dark mode background color + std::string dmcolor_; }; diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 2c9821cffa..607ea2dca6 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -990,9 +990,13 @@ string BufferParams::readToken(Lexer & lex, string const & token, } if (tok == "\\color") { lex.eatLine(); - string color = lex.getString(); + vector const colors = getVectorFromString(lex.getString(), " "); + string const lmcolor = colors.front(); + string dmcolor; + if (colors.size() > 1) + dmcolor = colors.back(); if (branch_ptr) - branch_ptr->setColor(color); + branch_ptr->setColors(lmcolor, dmcolor); } } } else if (token == "\\index") { @@ -1382,7 +1386,7 @@ void BufferParams::writeFile(ostream & os, Buffer const * buf) const os << "\\branch " << to_utf8(br.branch()) << "\n\\selected " << br.isSelected() << "\n\\filename_suffix " << br.hasFileNameSuffix() - << "\n\\color " << br.color() + << "\n\\color " << br.lightModeColor() << " " << br.darkModeColor() << "\n\\end_branch" << "\n"; } diff --git a/src/Color.cpp b/src/Color.cpp index 6248e67b6a..c627e5b654 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -153,6 +153,16 @@ RGBColor const RGBColorFromLaTeX(string const & color) } +RGBColor const inverseRGBColor(RGBColor color) +{ + color.r = 255 - color.r; + color.g = 255 - color.g; + color.b = 255 - color.b; + + return color; +} + + Color::Color(ColorCode base_color) : baseColor(base_color), mergeColor(Color_ignore) {} diff --git a/src/Color.h b/src/Color.h index 865f0ab8a8..d67c4989a7 100644 --- a/src/Color.h +++ b/src/Color.h @@ -64,6 +64,8 @@ RGBColor rgbFromHexName(std::string const & x11hexname); std::string const outputLaTeXColor(RGBColor const & color); /// Inverse of outputLaTeXColor RGBColor const RGBColorFromLaTeX(std::string const & color); +/// Inverted color +RGBColor const inverseRGBColor(RGBColor color); } // namespace lyx diff --git a/src/version.h b/src/version.h index d2ad51d619..1b15e8d6ab 100644 --- a/src/version.h +++ b/src/version.h @@ -32,8 +32,8 @@ extern char const * const lyx_version_info; // Do not remove the comment below, so we get merge conflict in // independent branches. Instead add your own. -#define LYX_FORMAT_LYX 603 // spitz: InsetGraphicsParam darkModeSensitive -#define LYX_FORMAT_TEX2LYX 603 +#define LYX_FORMAT_LYX 604 // spitz: separate dark branch color +#define LYX_FORMAT_TEX2LYX 604 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX #ifndef _MSC_VER