static int get_hex (int *start) { int value = convert_hex_digit (*start); int try; *start = readchar (timeout); while ((try = convert_hex_digit (*start)) >= 0) { value <<= 4; value += try; *start = readchar (timeout); } return value; } #if 0 /* Get N 32-bit words from remote, each preceded by a space, and put them in registers starting at REGNO. */ static void get_hex_regs (int n, int regno) { long val; int i; for (i = 0; i < n; i++) { int j; val = 0; for (j = 0; j < 8; j++) val = (val << 4) + get_hex_digit (j == 0); supply_register (regno++, (char *) &val); } }
paint::paint(const std::string& s) : color_attrib_(ColorAttrib::VALUE), backup_color_attrib_(ColorAttrib::NONE), opacity_(1.0) { if(s == "none") { color_attrib_ = ColorAttrib::NONE; } else if(s == "currentColor") { color_attrib_ = ColorAttrib::CURRENT_COLOR; } else if(s.length() > 1 && s[0] == '#') { ASSERT_LOG(s.length() == 4 || s.length() == 7, "Expected length of color definition to be 3 or 6 characters long, found: " << s.substr(1)); if(s.length() == 4) { int r_hex = convert_hex_digit(s[1]); int g_hex = convert_hex_digit(s[2]); int b_hex = convert_hex_digit(s[3]); color_value_ = color((r_hex << 4) | r_hex, (g_hex << 4) | g_hex, (b_hex << 4) | b_hex); } else { color_value_ = color((convert_hex_digit(s[1]) << 4) | convert_hex_digit(s[2]), (convert_hex_digit(s[3]) << 4) | convert_hex_digit(s[4]), (convert_hex_digit(s[5]) << 4) | convert_hex_digit(s[6])); } } else if(s.length() > 3 && s.substr(0,3) == "rgb") { boost::char_separator<char> seperators(" \n\t\r,()"); boost::tokenizer<boost::char_separator<char>> tok(s.substr(3), seperators); int count = 0; int cv[3]; for(auto it = tok.begin(); it != tok.end(); ++it) { char* end = NULL; long value = strtol(it->c_str(), &end, 10); uint8_t col_val = 0; if(value == 0 && end == it->c_str()) { ASSERT_LOG(false, "Unable to parse string as an integer: " << *it); } if(end != NULL && *end == '%') { ASSERT_LOG(value >= 0 && value <= 100, "Percentage values range from 0-100: " << value); col_val = uint8_t(value / 100.0 * 255); } else { ASSERT_LOG(value >= 0 && value <= 255, "Percentage values range from 0-255: " << value); col_val = uint8_t(value); } ASSERT_LOG(count < 3, "Too many numbers in color value"); cv[count] = col_val; } color_value_ = color(cv[0],cv[1],cv[2]); } else if(s.length() > 4 && s.substr(0, 4) == "url(") { auto st_it = std::find(s.begin(), s.end(), '('); auto ed_it = std::find(s.begin(), s.end(), '0'); color_ref_ = uri::uri::parse(std::string(st_it, ed_it)); color_attrib_ = ColorAttrib::FUNC_IRI; std::string backup(ed_it, s.end()); if(!backup.empty()) { // XXX parse stuff } } else if(s.length() > 9 && s.substr(0,9) == "icc-color") { boost::char_separator<char> seperators(" \n\t\r,()"); boost::tokenizer<boost::char_separator<char>> tok(s.substr(9), seperators); auto it = tok.begin(); icc_color_name_ = *it; for(; it != tok.end(); ++it) { try { double value = boost::lexical_cast<double>(*it); icc_color_values_.emplace_back(value); } catch(boost::bad_lexical_cast&) { ASSERT_LOG(false, "Unable to convert icc-color value from string to numeric: " << *it << " : " << s); } } color_attrib_ = ColorAttrib::ICC_COLOR; } else { color_value_ = color::from_name(s); } }
bool color_from_hex_string(const std::string& colstr, Color* value) { std::string s = colstr; if(s.empty()) { return false; } if(s[0] == '#') { s = s.substr(1); } if(s.length() != 3 && s.length() != 6 && s.length() != 8) { return false; } if(s.length() == 3) { int r_hex = 0, g_hex = 0, b_hex = 0; if(convert_hex_digit(s[0], &r_hex) && convert_hex_digit(s[1], &g_hex) && convert_hex_digit(s[2], &b_hex)) { *value = Color((r_hex << 4) | r_hex, (g_hex << 4) | g_hex, (b_hex << 4) | b_hex); return true; } return false; } int rh_hex = 0, rl_hex = 0, gh_hex = 0, gl_hex = 0, bh_hex = 0, bl_hex = 0; if(convert_hex_digit(s[0], &rh_hex) && convert_hex_digit(s[1], &rl_hex) && convert_hex_digit(s[2], &gh_hex) && convert_hex_digit(s[3], &gl_hex) && convert_hex_digit(s[4], &bh_hex) && convert_hex_digit(s[5], &bl_hex)) { int ah_hex = 0xf, al_hex = 0xf; if(s.length() == 8) { if(!convert_hex_digit(s[6], &ah_hex) || !convert_hex_digit(s[7], &al_hex)) { return false; } } *value = Color((rh_hex << 4) | rl_hex, (gh_hex << 4) | gl_hex, (bh_hex << 4) | bl_hex, (ah_hex << 4) | al_hex); return true; } return false; }