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-08-05 14:42:02 +10:00
|
|
|
static cruft::srgba4f
|
|
|
|
parse_hex (cruft::view<const char*> str)
|
2018-01-10 17:19:39 +11:00
|
|
|
{
|
|
|
|
if (str.size () != strlen ("#012345"))
|
|
|
|
throw std::invalid_argument ("expected length of 7");
|
|
|
|
|
|
|
|
if (str[0] != '#')
|
|
|
|
throw std::invalid_argument ("expected leading '#'");
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
if (!cruft::ascii::is_hex (str[1]) ||
|
|
|
|
!cruft::ascii::is_hex (str[2]) ||
|
|
|
|
!cruft::ascii::is_hex (str[3]) ||
|
|
|
|
!cruft::ascii::is_hex (str[4]) ||
|
|
|
|
!cruft::ascii::is_hex (str[5]) ||
|
|
|
|
!cruft::ascii::is_hex (str[6]))
|
2018-01-10 17:19:39 +11:00
|
|
|
{
|
|
|
|
throw std::invalid_argument ("expected hex digits");
|
2015-04-09 21:50:42 +10:00
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
uint8_t r = cruft::ascii::from_hex (str[1]) << 4u | cruft::ascii::from_hex (str[2]);
|
|
|
|
uint8_t g = cruft::ascii::from_hex (str[3]) << 4u | cruft::ascii::from_hex (str[4]);
|
|
|
|
uint8_t b = cruft::ascii::from_hex (str[5]) << 4u | cruft::ascii::from_hex (str[6]);
|
2015-04-09 21:50:42 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::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 <>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::srgba4f
|
|
|
|
cruft::parse<cruft::srgba4f> (cruft::view<const char*> str)
|
2017-10-10 16:50:15 +11:00
|
|
|
{
|
2018-01-10 17:19:39 +11:00
|
|
|
return parse_hex (str);
|
|
|
|
}
|