From 80946710cd05211f712bd6ddfaa853adff53da52 Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Sat, 14 Sep 2024 16:27:29 +0200 Subject: [PATCH] Core: Another attempt at DarkTheme improvement --- uppsrc/Core/Color.cpp | 43 ++++++++++++++++++++++++++++++ uppsrc/Core/Ops.h | 2 ++ uppsrc/Draw/Image.h | 1 - upptst/DarkTheme/main.cpp | 56 ++++++++++++++++++++++++++++++++------- 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/uppsrc/Core/Color.cpp b/uppsrc/Core/Color.cpp index 171a48fab..65d50398a 100644 --- a/uppsrc/Core/Color.cpp +++ b/uppsrc/Core/Color.cpp @@ -242,6 +242,47 @@ int Grayscale2(const Color& c) return (c.GetR() + c.GetG() + c.GetB()) / 3; } +#if 1 + +double C_R = 0.32; +double C_G = 0.5; +double C_B = 0.2; + +Color DarkTheme(Color color) +{ + double r = color.GetR(); + double g = color.GetG(); + double b = color.GetB(); + + double saturation = min(r, g, b); + + r -= saturation; + g -= saturation; + b -= saturation; + + auto Val = [&] { + return C_R * r + C_G * g + C_B * b; + }; + + double target = 255 - Saturate255(Val() + saturation); + double ratio = target / 128; + + double m = max(r, g, b); + + if(m * ratio >= 255) + ratio = 255 / m; + + r *= ratio; + g *= ratio; + b *= ratio; + saturation = target - Val(); + + return Color(Saturate255(r + saturation), Saturate255(g + saturation), Saturate255(b + saturation)); +} + + +#else // older worse algorithm + double DarkTheme_c[3] = { 0.3, 0.5, 0.2 }; int DarkTheme_middle = 155; @@ -315,6 +356,8 @@ Color DarkTheme(Color color) return Color((int)v[0], (int)v[1], (int)v[2]); } +#endif + Color DarkThemeCached(Color c) { const int N = 8; diff --git a/uppsrc/Core/Ops.h b/uppsrc/Core/Ops.h index 2cbb011a1..b349cefb9 100644 --- a/uppsrc/Core/Ops.h +++ b/uppsrc/Core/Ops.h @@ -161,6 +161,8 @@ inline dword FoldHash(dword h) #endif +inline byte Saturate255(int x) { return byte(~(x >> 24) & (x | (-(x >> 8) >> 24)) & 0xff); } + force_inline int SignificantBits(dword x) { // basically log2(x) + 1 except that for 0 this is 0, number of significant bits of x diff --git a/uppsrc/Draw/Image.h b/uppsrc/Draw/Image.h index e5ec41eb5..edb93ab05 100644 --- a/uppsrc/Draw/Image.h +++ b/uppsrc/Draw/Image.h @@ -61,7 +61,6 @@ String PackRLE(const RGBA *s, int len); inline int Grayscale(int r, int g, int b) { return (77 * r + 151 * g + 28 * b) >> 8; } inline int Grayscale(const RGBA& c) { return Grayscale(c.r, c.g, c.b); } -inline byte Saturate255(int x) { return byte(~(x >> 24) & (x | (-(x >> 8) >> 24)) & 0xff); } class Image; diff --git a/upptst/DarkTheme/main.cpp b/upptst/DarkTheme/main.cpp index 70d536057..a7b76205a 100644 --- a/upptst/DarkTheme/main.cpp +++ b/upptst/DarkTheme/main.cpp @@ -24,8 +24,9 @@ struct MyApp : TopWindow { }; namespace Upp { -extern double DarkTheme_c[]; -extern int DarkTheme_middle; +extern double C_R; +extern double C_G; +extern double C_B; }; void MyApp::Paint(Draw& w) @@ -53,7 +54,7 @@ void MyApp::Paint(Draw& w) << LtCyan() ; - w.DrawRect(GetSize(), Color(20, 20, 20)); + w.DrawRect(GetSize(), Black()); for(int i = 0; i < col.GetCount(); i++) { w.DrawRect(i * 32, 0, 32, 32, col[i]); w.DrawRect(i * 32, 35, 32, 32, DarkTheme(col[i])); @@ -68,12 +69,29 @@ void MyApp::Paint(Draw& w) } Size tsz = GetTextSize("Test", StdFont()); + int y = 200; w.DrawRect(0, 200, tsz.cx * col.GetCount(), tsz.cy, White()); for(int i = 0; i < col.GetCount(); i++) - w.DrawText(i * tsz.cx, 200, "Text", StdFont(), col[i]); + w.DrawText(i * tsz.cx, y, "Text", StdFont(), col[i]); + y += tsz.cy; for(int i = 0; i < col.GetCount(); i++) - w.DrawText(i * tsz.cx, 200 + tsz.cy, "Text", StdFont(), DarkTheme(col[i])); + w.DrawText(i * tsz.cx, y, "Text", StdFont(), DarkTheme(col[i])); + y += tsz.cy; +/* + for(int i = 0; i < col.GetCount(); i++) + w.DrawText(i * tsz.cx, y, "Text", StdFont(), DarkTheme2(col[i])); + y += tsz.cy; +*/ + y += tsz.cy; + + for(Color c : col) { + w.DrawRect(0, y, tsz.cy, tsz.cy, c); + c = DarkTheme(c); + w.DrawRect(tsz.cy + 10, y, tsz.cy, tsz.cy, c); + w.DrawText(2 * (tsz.cy + 10), y, AsString(c), StdFont(), White()); + y += tsz.cy; + } int x = tsz.cx * 32 + 50; w.DrawImage(x, 0, DarkImg::ide()); @@ -84,7 +102,6 @@ void MyApp::Paint(Draw& w) void MyApp::Sync() { ret.Retrieve(); - DarkTheme_c[2] = 1 - DarkTheme_c[0] - DarkTheme_c[1]; Refresh(); } @@ -98,18 +115,37 @@ MyApp::MyApp() w << [=] { Sync(); }; }; - Add(r.SetInc(0.02).MinMax(0, 1)); - Add(g.SetInc(0.02).MinMax(0, 1)); + Add(r.SetInc(0.01).MinMax(0, 1)); + Add(g.SetInc(0.01).MinMax(0, 1)); + Add(b.SetInc(0.01).MinMax(0, 1)); Add(middle); - + +/* ret(r, DarkTheme_c[0]) (g, DarkTheme_c[1]) (middle, DarkTheme_middle); - +*/ + + ret(r, C_R) + (g, C_G) + (b, C_B) + ; + Sizeable(); }; GUI_APP_MAIN { + DDUMP(DarkTheme(LtYellow())); + DDUMP(DarkTheme(Yellow())); + DDUMP(DarkTheme(Brown())); + DDUMP(DarkTheme(LtRed())); + DDUMP(DarkTheme(Black())); + DDUMP(DarkTheme(LtBlue())); + DDUMP(DarkTheme(LtBlue())); + DDUMP(DarkTheme(LtGreen())); + + DDUMP(DarkTheme(LtRed())); + DDUMP(DarkTheme(LtBlue())); MyApp().Run(); }