From 3686b745f1a9dbb4e8ea1d0fbde679f727b046d1 Mon Sep 17 00:00:00 2001 From: cxl Date: Wed, 8 Nov 2017 15:46:52 +0000 Subject: [PATCH] Core, CtrlLib: ColorFromText moved to Core git-svn-id: svn://ultimatepp.org/upp/trunk@11443 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Color.cpp | 28 ++++++++++++++++++++++++++++ uppsrc/Core/Color.h | 1 + uppsrc/Core/src.tpp/Color$en-us.tpp | 8 ++++++++ uppsrc/CtrlLib/ColorPopup.cpp | 29 ----------------------------- uppsrc/CtrlLib/DlgColor.h | 1 - 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/uppsrc/Core/Color.cpp b/uppsrc/Core/Color.cpp index 6ec9ecff3..bac71fd4a 100644 --- a/uppsrc/Core/Color.cpp +++ b/uppsrc/Core/Color.cpp @@ -159,6 +159,34 @@ String ColorToHtml(Color color) return Format("#%02X%02X%02X", color.GetR(), color.GetG(), color.GetB()); } +static int sCharFilterNoDigit(int c) +{ + return IsDigit(c) ? 0 : c; +} + +static int sCharFilterHex(int c) +{ + return c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' || IsDigit(c) ? c : 0; +} + +Color ColorFromText(const char *s) +{ + Vector h = Split(s, sCharFilterNoDigit); + if(h.GetCount() == 3 && (strchr(s, ',') || strchr(s, ';') || strchr(s, '.') || strchr(s, ' '))) { + int r = atoi(h[0]); + int g = atoi(h[1]); + int b = atoi(h[2]); + if(r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) + return Color(r, g, b); + } + String hex = Filter(s, sCharFilterHex); + if(hex.GetCount() == 6 || hex.GetCount() == 8) { + dword w = (dword)ScanInt64(~hex, NULL, 16); + return Color(byte(w >> 16), byte(w >> 8), byte(w)); + } + return Null; +} + Color Blend(Color c1, Color c2, int alpha) { int a = (alpha >> 7) + alpha; diff --git a/uppsrc/Core/Color.h b/uppsrc/Core/Color.h index 4784b8ff3..bc9c6cb1a 100644 --- a/uppsrc/Core/Color.h +++ b/uppsrc/Core/Color.h @@ -121,6 +121,7 @@ Color HsvColorf(double h, double s, double v); Color Blend(Color c1, Color c2, int alpha = 128); String ColorToHtml(Color color); +Color ColorFromText(const char *s); int Grayscale(const Color& c); bool IsDark(Color c); diff --git a/uppsrc/Core/src.tpp/Color$en-us.tpp b/uppsrc/Core/src.tpp/Color$en-us.tpp index e81eca143..aac4f9e58 100644 --- a/uppsrc/Core/src.tpp/Color$en-us.tpp +++ b/uppsrc/Core/src.tpp/Color$en-us.tpp @@ -296,6 +296,14 @@ form like #ffffff for white).&] [s7; [*/ Return value]-|HTML text.&] [s3; &] [s4;%- &] +[s5;:Upp`:`:ColorFromText`(const char`*`):%- [_^Upp`:`:Color^ Color]_[* ColorFromText]([@(0.0.255) c +onst]_[@(0.0.255) char]_`*[*@3 s])&] +[s2; Converts text to Color. Allowed formats are 3 decimal numbers +in range 0`-255, representing r, g, b colors and separated by +`',`' or `';`' or `'.`' or `' `', or the text must exactly contain +6 hexadecimal digits representing RRGGBB values.&] +[s3; &] +[s4;%- &] [s5;:GrayColor`(int`):%- [_^Color^ Color]_[* GrayColor]([@(0.0.255) int]_[*@3 a]_`=_[@3 128])&] [s2; Returns a gray color of intensity [%-*@3 a].&] [s3; &] diff --git a/uppsrc/CtrlLib/ColorPopup.cpp b/uppsrc/CtrlLib/ColorPopup.cpp index e850b6a42..e16d1d9c1 100644 --- a/uppsrc/CtrlLib/ColorPopup.cpp +++ b/uppsrc/CtrlLib/ColorPopup.cpp @@ -98,35 +98,6 @@ Color ReadColor(CParser& p) return Color(minmax(r, 0, 255), minmax(g, 0, 255), minmax(b, 0, 255)); } - -static int sCharFilterNoDigit(int c) -{ - return IsDigit(c) ? 0 : c; -} - -static int sCharFilterHex(int c) -{ - return c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' || IsDigit(c) ? c : 0; -} - -Color ColorFromText(const char *s) -{ - Vector h = Split(s, sCharFilterNoDigit); - if(h.GetCount() == 3 && (strchr(s, ',') || strchr(s, ';') || strchr(s, '.') || strchr(s, ' '))) { - int r = atoi(h[0]); - int g = atoi(h[1]); - int b = atoi(h[2]); - if(r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) - return Color(r, g, b); - } - String hex = Filter(s, sCharFilterHex); - if(hex.GetCount() == 6 || hex.GetCount() == 8) { - dword w = (dword)ScanInt64(~hex, NULL, 16); - return Color(byte(w >> 16), byte(w >> 8), byte(w)); - } - return Null; -} - ColorPopUp::~ColorPopUp() {} int ColorPopUp::GetColorCount() const diff --git a/uppsrc/CtrlLib/DlgColor.h b/uppsrc/CtrlLib/DlgColor.h index 828cde38b..e90592d52 100644 --- a/uppsrc/CtrlLib/DlgColor.h +++ b/uppsrc/CtrlLib/DlgColor.h @@ -273,6 +273,5 @@ public: String FormatColor(Color c); Color ReadColor(CParser& p); -Color ColorFromText(const char *s); #endif//__TCtrlLib_DlgColor__