/* * 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/. * * Copyright 2010-2017 Danny Robson */ #include "colour.hpp" #include "ascii.hpp" #include "parse.hpp" /////////////////////////////////////////////////////////////////////////////// static util::srgba4f parse_hex (util::view 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"); } 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]); return util::srgba<4,uint8_t> { r, g, b, 255 }.template cast (); } /////////////////////////////////////////////////////////////////////////////// template <> util::srgba4f util::parse (util::view str) { return parse_hex (str); }