color collection, now only CSS colors
PIVariantTypes::Color from/to string (with CSS names)
This commit is contained in:
194
libs/main/types/colors.cpp
Normal file
194
libs/main/types/colors.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
CSS colors
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "colors_p.h"
|
||||
|
||||
#define CSS_COLOR(n,v) {#n,v}
|
||||
|
||||
struct CSSColor {
|
||||
const char * name;
|
||||
uint color;
|
||||
} css_colors[] = {
|
||||
CSS_COLOR(aliceblue ,0xf0f8ff),
|
||||
CSS_COLOR(antiquewhite ,0xfaebd7),
|
||||
CSS_COLOR(aqua ,0x00ffff),
|
||||
CSS_COLOR(aquamarine ,0x7fffd4),
|
||||
CSS_COLOR(azure ,0xf0ffff),
|
||||
CSS_COLOR(beige ,0xf5f5dc),
|
||||
CSS_COLOR(bisque ,0xffe4c4),
|
||||
CSS_COLOR(black ,0x000000),
|
||||
CSS_COLOR(blanchedalmond ,0xffebcd),
|
||||
CSS_COLOR(blue ,0x0000ff),
|
||||
CSS_COLOR(blueviolet ,0x8a2be2),
|
||||
CSS_COLOR(brown ,0xa52a2a),
|
||||
CSS_COLOR(burlywood ,0xdeb887),
|
||||
CSS_COLOR(cadetblue ,0x5f9ea0),
|
||||
CSS_COLOR(chartreuse ,0x7fff00),
|
||||
CSS_COLOR(chocolate ,0xd2691e),
|
||||
CSS_COLOR(coral ,0xff7f50),
|
||||
CSS_COLOR(cornflowerblue ,0x6495ed),
|
||||
CSS_COLOR(cornsilk ,0xfff8dc),
|
||||
CSS_COLOR(crimson ,0xdc143c),
|
||||
CSS_COLOR(cyan ,0x00ffff),
|
||||
CSS_COLOR(darkblue ,0x00008b),
|
||||
CSS_COLOR(darkcyan ,0x008b8b),
|
||||
CSS_COLOR(darkgoldenrod ,0xb8860b),
|
||||
CSS_COLOR(darkgray ,0xa9a9a9),
|
||||
CSS_COLOR(darkgreen ,0x006400),
|
||||
CSS_COLOR(darkgrey ,0xa9a9a9),
|
||||
CSS_COLOR(darkkhaki ,0xbdb76b),
|
||||
CSS_COLOR(darkmagenta ,0x8b008b),
|
||||
CSS_COLOR(darkolivegreen ,0x556b2f),
|
||||
CSS_COLOR(darkorange ,0xff8c00),
|
||||
CSS_COLOR(darkorchid ,0x9932cc),
|
||||
CSS_COLOR(darkred ,0x8b0000),
|
||||
CSS_COLOR(darksalmon ,0xe9967a),
|
||||
CSS_COLOR(darkseagreen ,0x8fbc8f),
|
||||
CSS_COLOR(darkslateblue ,0x483d8b),
|
||||
CSS_COLOR(darkslategray ,0x2f4f4f),
|
||||
CSS_COLOR(darkslategrey ,0x2f4f4f),
|
||||
CSS_COLOR(darkturquoise ,0x00ced1),
|
||||
CSS_COLOR(darkviolet ,0x9400d3),
|
||||
CSS_COLOR(deeppink ,0xff1493),
|
||||
CSS_COLOR(deepskyblue ,0x00bfff),
|
||||
CSS_COLOR(dimgray ,0x696969),
|
||||
CSS_COLOR(dimgrey ,0x696969),
|
||||
CSS_COLOR(dodgerblue ,0x1e90ff),
|
||||
CSS_COLOR(firebrick ,0xb22222),
|
||||
CSS_COLOR(floralwhite ,0xfffaf0),
|
||||
CSS_COLOR(forestgreen ,0x228b22),
|
||||
CSS_COLOR(fuchsia ,0xff00ff),
|
||||
CSS_COLOR(gainsboro ,0xdcdcdc),
|
||||
CSS_COLOR(ghostwhite ,0xf8f8ff),
|
||||
CSS_COLOR(gold ,0xffd700),
|
||||
CSS_COLOR(goldenrod ,0xdaa520),
|
||||
CSS_COLOR(gray ,0x808080),
|
||||
CSS_COLOR(green ,0x008000),
|
||||
CSS_COLOR(greenyellow ,0xadff2f),
|
||||
CSS_COLOR(grey ,0x808080),
|
||||
CSS_COLOR(honeydew ,0xf0fff0),
|
||||
CSS_COLOR(hotpink ,0xff69b4),
|
||||
CSS_COLOR(indianred ,0xcd5c5c),
|
||||
CSS_COLOR(indigo ,0x4b0082),
|
||||
CSS_COLOR(ivory ,0xfffff0),
|
||||
CSS_COLOR(khaki ,0xf0e68c),
|
||||
CSS_COLOR(lavender ,0xe6e6fa),
|
||||
CSS_COLOR(lavenderblush ,0xfff0f5),
|
||||
CSS_COLOR(lawngreen ,0x7cfc00),
|
||||
CSS_COLOR(lemonchiffon ,0xfffacd),
|
||||
CSS_COLOR(lightblue ,0xadd8e6),
|
||||
CSS_COLOR(lightcoral ,0xf08080),
|
||||
CSS_COLOR(lightcyan ,0xe0ffff),
|
||||
CSS_COLOR(lightgoldenrodyellow ,0xfafad2),
|
||||
CSS_COLOR(lightgray ,0xd3d3d3),
|
||||
CSS_COLOR(lightgreen ,0x90ee90),
|
||||
CSS_COLOR(lightgrey ,0xd3d3d3),
|
||||
CSS_COLOR(lightpink ,0xffb6c1),
|
||||
CSS_COLOR(lightsalmon ,0xffa07a),
|
||||
CSS_COLOR(lightseagreen ,0x20b2aa),
|
||||
CSS_COLOR(lightskyblue ,0x87cefa),
|
||||
CSS_COLOR(lightslategray ,0x778899),
|
||||
CSS_COLOR(lightslategrey ,0x778899),
|
||||
CSS_COLOR(lightsteelblue ,0xb0c4de),
|
||||
CSS_COLOR(lightyellow ,0xffffe0),
|
||||
CSS_COLOR(lime ,0x00ff00),
|
||||
CSS_COLOR(limegreen ,0x32cd32),
|
||||
CSS_COLOR(linen ,0xfaf0e6),
|
||||
CSS_COLOR(magenta ,0xff00ff),
|
||||
CSS_COLOR(maroon ,0x800000),
|
||||
CSS_COLOR(mediumaquamarine ,0x66cdaa),
|
||||
CSS_COLOR(mediumblue ,0x0000cd),
|
||||
CSS_COLOR(mediumorchid ,0xba55d3),
|
||||
CSS_COLOR(mediumpurple ,0x9370db),
|
||||
CSS_COLOR(mediumseagreen ,0x3cb371),
|
||||
CSS_COLOR(mediumslateblue ,0x7b68ee),
|
||||
CSS_COLOR(mediumspringgreen ,0x00fa9a),
|
||||
CSS_COLOR(mediumturquoise ,0x48d1cc),
|
||||
CSS_COLOR(mediumvioletred ,0xc71585),
|
||||
CSS_COLOR(midnightblue ,0x191970),
|
||||
CSS_COLOR(mintcream ,0xf5fffa),
|
||||
CSS_COLOR(mistyrose ,0xffe4e1),
|
||||
CSS_COLOR(moccasin ,0xffe4b5),
|
||||
CSS_COLOR(navajowhite ,0xffdead),
|
||||
CSS_COLOR(navy ,0x000080),
|
||||
CSS_COLOR(oldlace ,0xfdf5e6),
|
||||
CSS_COLOR(olive ,0x808000),
|
||||
CSS_COLOR(olivedrab ,0x6b8e23),
|
||||
CSS_COLOR(orange ,0xffa500),
|
||||
CSS_COLOR(orangered ,0xff4500),
|
||||
CSS_COLOR(orchid ,0xda70d6),
|
||||
CSS_COLOR(palegoldenrod ,0xeee8aa),
|
||||
CSS_COLOR(palegreen ,0x98fb98),
|
||||
CSS_COLOR(paleturquoise ,0xafeeee),
|
||||
CSS_COLOR(palevioletred ,0xdb7093),
|
||||
CSS_COLOR(papayawhip ,0xffefd5),
|
||||
CSS_COLOR(peachpuff ,0xffdab9),
|
||||
CSS_COLOR(peru ,0xcd853f),
|
||||
CSS_COLOR(pink ,0xffc0cb),
|
||||
CSS_COLOR(plum ,0xdda0dd),
|
||||
CSS_COLOR(powderblue ,0xb0e0e6),
|
||||
CSS_COLOR(purple ,0x800080),
|
||||
CSS_COLOR(red ,0xff0000),
|
||||
CSS_COLOR(rosybrown ,0xbc8f8f),
|
||||
CSS_COLOR(royalblue ,0x4169e1),
|
||||
CSS_COLOR(saddlebrown ,0x8b4513),
|
||||
CSS_COLOR(salmon ,0xfa8072),
|
||||
CSS_COLOR(sandybrown ,0xf4a460),
|
||||
CSS_COLOR(seagreen ,0x2e8b57),
|
||||
CSS_COLOR(seashell ,0xfff5ee),
|
||||
CSS_COLOR(sienna ,0xa0522d),
|
||||
CSS_COLOR(silver ,0xc0c0c0),
|
||||
CSS_COLOR(skyblue ,0x87ceeb),
|
||||
CSS_COLOR(slateblue ,0x6a5acd),
|
||||
CSS_COLOR(slategray ,0x708090),
|
||||
CSS_COLOR(slategrey ,0x708090),
|
||||
CSS_COLOR(snow ,0xfffafa),
|
||||
CSS_COLOR(springgreen ,0x00ff7f),
|
||||
CSS_COLOR(steelblue ,0x4682b4),
|
||||
CSS_COLOR(tan ,0xd2b48c),
|
||||
CSS_COLOR(teal ,0x008080),
|
||||
CSS_COLOR(thistle ,0xd8bfd8),
|
||||
CSS_COLOR(tomato ,0xff6347),
|
||||
CSS_COLOR(turquoise ,0x40e0d0),
|
||||
CSS_COLOR(violet ,0xee82ee),
|
||||
CSS_COLOR(wheat ,0xf5deb3),
|
||||
CSS_COLOR(white ,0xffffff),
|
||||
CSS_COLOR(whitesmoke ,0xf5f5f5),
|
||||
CSS_COLOR(yellow ,0xffff00),
|
||||
CSS_COLOR(yellowgreen ,0x9acd32),
|
||||
{nullptr, 0}
|
||||
};
|
||||
|
||||
#undef CSS_COLOR
|
||||
|
||||
|
||||
PIColorCollection & PIColorCollection::instance() {
|
||||
static PIColorCollection ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIColorCollection::PIColorCollection() {
|
||||
CSSColor * c = css_colors;
|
||||
while (c->name) {
|
||||
css[c->name] = c->color | 0xFF000000;
|
||||
++c;
|
||||
}
|
||||
}
|
||||
|
||||
35
libs/main/types/colors_p.h
Normal file
35
libs/main/types/colors_p.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Color collection
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef colors_p_H
|
||||
#define colors_p_H
|
||||
|
||||
#include "pivarianttypes.h"
|
||||
|
||||
|
||||
class PIColorCollection {
|
||||
public:
|
||||
static PIColorCollection & instance();
|
||||
PIVariantTypes::Color getCSS(const PIString & name) const {return css.value(name);}
|
||||
private:
|
||||
PIColorCollection();
|
||||
PIMap<PIString, PIVariantTypes::Color> css;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -110,7 +110,7 @@ void PIVariant::setValueFromString(const PIString & v) {
|
||||
case PIVariant::pivEnum: {PIVariantTypes::Enum r = toEnum(); r.selectName(v); setValue(r);} break;
|
||||
case PIVariant::pivFile: {PIVariantTypes::File r = toFile(); r.file = v; setValue(r);} break;
|
||||
case PIVariant::pivDir: {PIVariantTypes::Dir r = toDir(); r.dir = v; setValue(r);} break;
|
||||
case PIVariant::pivColor: {if (v.startsWith('#')) setValue(PIVariantTypes::Color(v.mid(1).toUInt(16))); else setValue(PIVariantTypes::Color(v.toUInt()));} break;
|
||||
case PIVariant::pivColor: {setValue(PIVariantTypes::Color::fromString(v));} break;
|
||||
case PIVariant::pivIODevice: {setValue(PIVariantTypes::IODevice());} break; // TODO
|
||||
case PIVariant::pivPoint: {PIStringList l = v.split(';'); if (l.size() >= 2) setValue(PIPointd(l[0].toDouble(), l[1].toDouble()));} break;
|
||||
case PIVariant::pivRect: {PIStringList l = v.split(';'); if (l.size() >= 4) setValue(PIRectd(l[0].toDouble(), l[1].toDouble(), l[2].toDouble(), l[3].toDouble()));} break;
|
||||
@@ -822,7 +822,7 @@ PIString PIVariant::toString() const {
|
||||
case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return r.selectedName();}
|
||||
case PIVariant::pivFile: {PIVariantTypes::File r; ba >> r; return r.file;}
|
||||
case PIVariant::pivDir: {PIVariantTypes::Dir r; ba >> r; return r.dir;}
|
||||
case PIVariant::pivColor: {PIVariantTypes::Color r; ba >> r; return "0x" + PIString::fromNumber(r.rgba, 16);}
|
||||
case PIVariant::pivColor: {PIVariantTypes::Color r; ba >> r; return r.toString();}
|
||||
case PIVariant::pivIODevice: {PIVariantTypes::IODevice r; ba >> r; return "IODevice";} // TODO
|
||||
case PIVariant::pivPoint: {PIPointd r; ba >> r; return PIString::fromNumber(r.x) + ";" + PIString::fromNumber(r.y);} break;
|
||||
case PIVariant::pivRect: {PIRectd r; ba >> r; return PIString::fromNumber(r.left()) + ";" + PIString::fromNumber(r.bottom()) + ";" + PIString::fromNumber(r.width()) + ";" + PIString::fromNumber(r.height());} break;
|
||||
@@ -990,9 +990,7 @@ PIVariantTypes::Dir PIVariant::toDir() const {
|
||||
//!
|
||||
PIVariantTypes::Color PIVariant::toColor() const {
|
||||
PIByteArray ba(_content);
|
||||
if (_type == PIVariant::pivString) {PIString r; ba >> r;
|
||||
if (r.startsWith('#')) return PIVariantTypes::Color(r.mid(1).toUInt(16));
|
||||
else return PIVariantTypes::Color(r.toUInt());}
|
||||
if (_type == PIVariant::pivString) {PIString r; ba >> r; return PIVariantTypes::Color::fromString(r);}
|
||||
if (_type == PIVariant::pivColor) {PIVariantTypes::Color r; ba >> r; return r;}
|
||||
if (_type == PIVariant::pivInt) {int v; ba >> v; return PIVariantTypes::Color(v);}
|
||||
if (_type == PIVariant::pivCustom) {return getAsValue<PIVariantTypes::Color>(*this);}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "pivarianttypes.h"
|
||||
#include "pipropertystorage.h"
|
||||
#include "colors_p.h"
|
||||
#ifndef MICRO_PIP
|
||||
# include "piiodevice.h"
|
||||
#endif
|
||||
@@ -156,3 +157,18 @@ PIVariantTypes::Enum & PIVariantTypes::Enum::operator <<(const PIStringList & v)
|
||||
(*this) << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PIVariantTypes::Color PIVariantTypes::Color::fromString(const PIString & str) {
|
||||
if (str.size_s() < 2) return Color();
|
||||
PIString cn = str.toLowerCase().trim();
|
||||
if (cn.startsWith('#')) return Color(str.mid(1).toUInt(16));
|
||||
if (cn.startsWith("0x")) return Color(str.mid(2).toUInt(16));
|
||||
return PIColorCollection::instance().getCSS(cn);
|
||||
}
|
||||
|
||||
|
||||
PIString PIVariantTypes::Color::toString() const {
|
||||
return "#" + PIString::fromNumber(rgba, 16);
|
||||
}
|
||||
|
||||
@@ -233,6 +233,14 @@ struct PIP_EXPORT Dir {
|
||||
struct PIP_EXPORT Color {
|
||||
Color(uint v = 0) {rgba = v;}
|
||||
|
||||
//! \~english Returns color from #HEX, 0xHEX or name.
|
||||
//! \~russian Возвращает цвет от #HEX, 0xHEX или имени.
|
||||
static Color fromString(const PIString & str);
|
||||
|
||||
//! \~english Returns color as #HEX.
|
||||
//! \~russian Возвращает цвет как #HEX.
|
||||
PIString toString() const;
|
||||
|
||||
//! \~english Integer color.
|
||||
//! \~russian Целочисленный цвет.
|
||||
uint rgba;
|
||||
|
||||
Reference in New Issue
Block a user