sfx2/source/dialog/StyleList.cxx |   42 ++++++++++-----------------------------
 1 file changed, 11 insertions(+), 31 deletions(-)

New commits:
commit 2ff85183b959c05134a2737d6d14afd1882f2ba3
Author:     Jim Raykowski <rayk...@gmail.com>
AuthorDate: Wed Apr 26 17:16:20 2023 -0800
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Apr 27 07:36:30 2023 +0200

    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 <qui...@gmail.com>

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

Reply via email to