Core, CtrlLib: ColorFromText moved to Core

git-svn-id: svn://ultimatepp.org/upp/trunk@11443 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2017-11-08 15:46:52 +00:00
parent 35e344d7d0
commit 3686b745f1
5 changed files with 37 additions and 30 deletions

View file

@ -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<String> 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;

View file

@ -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);

View file

@ -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; &]

View file

@ -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<String> 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

View file

@ -273,6 +273,5 @@ public:
String FormatColor(Color c);
Color ReadColor(CParser& p);
Color ColorFromText(const char *s);
#endif//__TCtrlLib_DlgColor__