2011-09-13 15:14:12 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-09-13 15:14:12 +10:00
|
|
|
*
|
2017-06-08 15:48:15 +10:00
|
|
|
* Copyright 2010-2017 Danny Robson <danny@nerdcruft.net>
|
2011-09-13 15:14:12 +10:00
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "colour.hpp"
|
2013-08-05 16:37:11 +10:00
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
#include "ascii.hpp"
|
|
|
|
#include "parse.hpp"
|
2011-09-13 15:14:12 +10:00
|
|
|
|
|
|
|
|
2017-05-22 16:20:21 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-10 17:19:39 +11:00
|
|
|
static util::srgba4f
|
|
|
|
parse_hex (util::view<const char*> str)
|
|
|
|
{
|
|
|
|
if (str.size () != strlen ("#012345"))
|
|
|
|
throw std::invalid_argument ("expected length of 7");
|
|
|
|
|
|
|
|
if (str[0] != '#')
|
|
|
|
throw std::invalid_argument ("expected leading '#'");
|
|
|
|
|
|
|
|
if (!util::ascii::is_hex (str[1]) ||
|
|
|
|
!util::ascii::is_hex (str[2]) ||
|
|
|
|
!util::ascii::is_hex (str[3]) ||
|
|
|
|
!util::ascii::is_hex (str[4]) ||
|
|
|
|
!util::ascii::is_hex (str[5]) ||
|
|
|
|
!util::ascii::is_hex (str[6]))
|
|
|
|
{
|
|
|
|
throw std::invalid_argument ("expected hex digits");
|
2015-04-09 21:50:42 +10:00
|
|
|
}
|
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
uint8_t r = util::ascii::from_hex (str[1]) << 4u | util::ascii::from_hex (str[2]);
|
|
|
|
uint8_t g = util::ascii::from_hex (str[3]) << 4u | util::ascii::from_hex (str[4]);
|
|
|
|
uint8_t b = util::ascii::from_hex (str[5]) << 4u | util::ascii::from_hex (str[6]);
|
2015-04-09 21:50:42 +10:00
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
return util::srgba<4,uint8_t> { r, g, b, 255 }.template cast<float> ();
|
2015-04-09 21:50:42 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-13 15:14:12 +10:00
|
|
|
|
2016-04-02 13:37:09 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-10 17:19:39 +11:00
|
|
|
template <>
|
|
|
|
util::srgba4f
|
|
|
|
util::parse<util::srgba4f> (util::view<const char*> str)
|
2017-10-10 16:50:15 +11:00
|
|
|
{
|
2018-01-10 17:19:39 +11:00
|
|
|
return parse_hex (str);
|
|
|
|
}
|