/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#include <iomanip>
#include <sstream>
#include <string>

#include "Color.h"
#include "StringParser.h"

namespace Blinker {

/// constructor
Color::Color():
  m_red(0),
  m_green(0),
  m_blue(0)
{
}

/**
 * @brief parse from string
 * @param[in] str color as string in web format
 * @return if parsing was successful
 */
bool Color::fromStr(const std::string &str)
{
  StringParser parser(str);
  unsigned int red, green, blue;
  if (!parser.uintHex(2, red) ||
      !parser.uintHex(2, green) ||
      !parser.uintHex(2, blue) ||
      !parser.isDone()) {
    return false;
  }
  m_red = red;
  m_green = green;
  m_blue = blue;
  return true;
}

/**
 * @brief convert to string
 * @return color as string in web format
 */
std::string Color::toStr() const
{
  std::stringstream strm;
  strm << std::hex << std::setw(2) << std::setfill('0')
       << m_red << m_green << m_blue;
  return strm.str();
}

} // namespace Blinker

