#include "Color.h"

Color::Color(unsigned char r, unsigned char g, unsigned char b, double a)
    : mR(r),
      mG(g),
      mB(b),
      mAlpha(a)
{

}

Color::Color(const std::string& hexString)
{
    if (hexString.size() < 7)
    {
        return;
    }
    if (hexString[0] != '#')
    {
        return;
    }
    mR = toDecimal(hexString.substr(1, 2));
    mG = toDecimal(hexString.substr(3, 2));
    mB = toDecimal(hexString.substr(5, 2));
}

unsigned char Color::toDecimal(const std::string& hex) const
{
    return static_cast<unsigned char>(std::stoul("0x" + hex, nullptr, 16));
}

std::unique_ptr<Color> Color::Create(unsigned char r, unsigned char g, unsigned char b, double a )
{
    return std::make_unique<Color>(r, g, b, a);
}

std::unique_ptr<Color> Color::Create(const Color& color)
{
    return std::make_unique<Color>(color);
}

unsigned char Color::getR() const
{
    return mR;
}

unsigned char Color::getG() const
{
    return mG;
}

unsigned char Color::getB() const
{
    return mB;
}

double Color::getAlpha() const
{
    return mAlpha;
}

std::vector<double> Color::getAsVectorDouble() const
{
    return { mR / 255.0, mG / 255.0, mB / 255.0, mAlpha };
}

uint32_t Color::getAsUInt32() const
{
    return static_cast<uint32_t>(mB + (mG << 8) + (mR << 16));
}

std::string Color::toString() const
{
    return std::to_string(static_cast<int>(mR)) + "," + std::to_string(static_cast<int>(mG)) + "," + std::to_string(static_cast<int>(mB));
}