Rev 8 | Blame | Last modification | View Log | RSS feed
/*
* Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <iostream>
#include <iomanip>
#include "tcolor.h"
#include "terror.h"
TPalette *TColor::mPalette = nullptr;
using std::string;
using std::setw;
using std::setfill;
using std::hex;
using std::dec;
using std::resetiosflags;
TColor::COLOR_T TColor::getAMXColor(const string& color)
{
DECL_TRACER("TColor::getAMXColor(const string& color)");
if (color.empty())
return TColor::COLOR_T();
size_t pos = color.find('#');
if (pos == string::npos)
{
if (!mPalette)
{
MSG_ERROR("No palette was set! First set a palette to be able to get any color!");
TError::setError();
return TColor::COLOR_T();
}
PDATA_T pd = mPalette->findColor(color);
if (!pd.name.empty())
{
MSG_DEBUG("Found color " << color << ": #" << setw(8) << setfill('0') << hex << pd.color);
return splitColors(pd);
}
MSG_DEBUG("Color " << color << " not found!");
return TColor::COLOR_T();
}
TColor::COLOR_T ct;
ct.red = (int)strtol(color.substr(pos+1, 2).c_str(), NULL, 16);
ct.green = (int)strtol(color.substr(pos+3, 2).c_str(), NULL, 16);
ct.blue = (int)strtol(color.substr(pos+5, 2).c_str(), NULL, 16);
if (color.length() > 7)
ct.alpha = (int)strtol(color.substr(pos+7).c_str(), NULL, 16);
else
ct.alpha = 0x00ff;
MSG_DEBUG("Splitted value " << color << " into "
<< setw(2) << setfill('0') << hex << ct.red << ":"
<< setw(2) << setfill('0') << hex << ct.green << ":"
<< setw(2) << setfill('0') << hex << ct.blue << ":"
<< setw(2) << setfill('0') << hex << ct.alpha);
return ct;
}
TColor::COLOR_T TColor::splitColors(PDATA_T& pd)
{
DECL_TRACER("TColor::splitColors(PDATA_T& pd)");
TColor::COLOR_T ct;
if (pd.color > 0x00ffffff)
{
ct.red = (pd.color & 0xff000000) >> 24;
ct.green = (pd.color & 0x00ff0000) >> 16;
ct.blue = (pd.color & 0x0000ff00) >> 8;
ct.alpha = (pd.color & 0x000000ff);
}
else
{
ct.red = (pd.color & 0xff000000) >> 24;
ct.green = (pd.color & 0x00ff0000) >> 16;
ct.blue = (pd.color & 0x0000ff00) >> 8;
ct.alpha = 0x00ff;
}
return ct;
}
SkColor TColor::getSkiaColor(const std::string& color)
{
DECL_TRACER("TColor::getSkiaColor(const std::string& color)");
COLOR_T col = getAMXColor(color);
return SkColorSetARGB(col.alpha, col.red, col.green, col.blue);
}
ulong TColor::getColor(const std::string& color)
{
DECL_TRACER("TColor::getColor(const std::string& color)");
TColor::COLOR_T ct = getAMXColor(color);
ulong col = ((ct.red << 24) & 0xff000000) | ((ct.green << 16) & 0x00ff0000) | ((ct.blue << 8) & 0x0000ff00) | (ct.alpha & 0x000000ff);
return col;
}
std::string TColor::colorToString(ulong color)
{
PDATA_T pd;
pd.color = color;
TColor::COLOR_T col = splitColors(pd);
std::stringstream s;
s << std::setw(2) << std::setfill('0') << std::hex << col.red << ":";
s << std::setw(2) << std::setfill('0') << std::hex << col.green << ":";
s << std::setw(2) << std::setfill('0') << std::hex << col.blue << ":";
s << std::setw(2) << std::setfill('0') << std::hex << col.alpha;
std::string ret = s.str();
return ret;
}
std::string TColor::colorToString(SkColor color)
{
std::stringstream s;
s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetR(color) << ":";
s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetG(color) << ":";
s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetB(color) << ":";
s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetA(color);
std::string ret = s.str();
return ret;
}
int TColor::setAlpha(ulong color, int alpha)
{
DECL_TRACER("TColor::setAlpha(ulong color, int alpha)");
if (alpha > 0x00ff)
alpha = 0x00ff;
else if (alpha < 0)
alpha = 0;
return ((color & 0xffffff00) | (alpha & 0x00ff));
}
SkColor TColor::setAlpha(SkColor color, int alpha)
{
DECL_TRACER("TColor::setAlpha(SkColor color, int alpha)");
if (alpha > 0x00ff)
alpha = 0x00ff;
else if (alpha < 0)
alpha = 0;
SkColor red = SkColorGetR(color);
SkColor green = SkColorGetG(color);
SkColor blue = SkColorGetB(color);
return SkColorSetARGB(alpha, red, green, blue);
}
int TColor::calcAlpha(int alpha1, int alpha2)
{
DECL_TRACER("TColor::calcAlpha(int alpha1, int alpha2)");
return (alpha1 + alpha2) / 2;
}
ulong TColor::setAlphaTreshold(ulong color, int alpha)
{
DECL_TRACER("TColor::setAlphaTreshold(ulong color, int alpha)");
uint al = color & 0x000000ff;
if (al > (uint)alpha)
return (color & 0xffffff00) | (alpha & 0x00ff);
return color;
}
SkColor TColor::setAlphaTreshold(SkColor color, int alpha)
{
DECL_TRACER("TColor::setAlphaTreshold(SkColor color, int alpha)");
int al = SkColorGetA(color);
if (al > alpha)
return SkColorSetA(color, alpha);
return color;
}