diff options
author | Jim Raykowski <raykowj@gmail.com> | 2023-04-26 17:16:20 -0800 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2023-04-27 07:36:30 +0200 |
commit | 2ff85183b959c05134a2737d6d14afd1882f2ba3 (patch) | |
tree | dfed46a57ae2df641b46ac44977e17bcce856362 | |
parent | 0340cbbc34d2c97baa5b7ebc4a57650861907067 (diff) |
tdf#38194 Use a hashed string HSL color approach
for style name color creation
New magic for style name color creation.
Change-Id: Ia6eec2dcf04fc2c3c38276bf6eaed73598505947
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151079
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | sfx2/source/dialog/StyleList.cxx | 42 |
1 files changed, 11 insertions, 31 deletions
diff --git a/sfx2/source/dialog/StyleList.cxx b/sfx2/source/dialog/StyleList.cxx index ce2d2f9bb7d0..7b17137363a7 100644 --- a/sfx2/source/dialog/StyleList.cxx +++ b/sfx2/source/dialog/StyleList.cxx @@ -63,6 +63,7 @@ #include <StyleList.hxx> #include <vcl/virdev.hxx> +#include <basegfx/color/bcolortools.hxx> using namespace css; using namespace css::beans; @@ -94,42 +95,21 @@ public: namespace { -Color ColorHash(const OUString& rString) +Color ColorHash(std::u16string_view rString) { - // method 1 - //Color aColor(rString.hashCode() & 0xFFFFFF); + static std::vector aSaturationArray{ 0.90, 0.75, 0.60 }; + static std::vector aLightnessArray = aSaturationArray; - // method 2 borrowed from https://github.com/RolandR/ColorHash/blob/master/colorhash.js - //sal_Int32 nSum = 0; + sal_uInt32 nStringHash = OUString(rString).hashCode(); - //for (int i = 0; i < rString.getLength(); i++) - //{ - // nSum += rString[i]; - //} + double nHue = nStringHash % 359; + double nSaturation = aSaturationArray[nStringHash / 360 % aSaturationArray.size()]; + double nLightness + = aLightnessArray[nStringHash / 360 / aSaturationArray.size() % aLightnessArray.size()]; - //sal_uInt8 nRed = OUString("0." + OUString(OUString::number(std::sin(nSum + 1))).copy(6)).toDouble() * 256; - //sal_uInt8 nGreen = OUString("0." + OUString(OUString::number(std::sin(nSum + 2))).copy(6)).toDouble() * 256; - //sal_uInt8 nBlue = OUString("0." + OUString(OUString::number(std::sin(nSum + 3))).copy(6)).toDouble() * 256; + basegfx::BColor aHSLColor(nHue, nSaturation, nLightness); - //Color aColor(nRed, nGreen, nBlue); - - // method 3 std::hash - //const std::hash<OUString> hasher; - //Color aColor(hasher(rString) & 0xFFFFFF); - - // method 4 - sal_uInt32 nLen = rString.getLength(); - sal_uInt32 nHashCode = nLen; - while (nLen > 0) - { - nHashCode = (nHashCode * 107) + rString[nLen - 1]; - nLen--; - } - sal_uInt32 nColor = nHashCode & 0xFFFFFF; - Color aColor(ColorTransparency, nColor); - aColor.ApplyTintOrShade(5000); - - return aColor; + return Color(basegfx::utils::hsl2rgb(aHSLColor)); } // used to disallow the default character style in the styles highlighter character styles color map |