RGB8Color ColorRampLookup(const short h, const ColorRamp* ramp_colors, const int numramp, const unsigned char interp_levels) { assert(ramp_colors != NULL); assert(numramp >= 2); unsigned short f, of; unsigned short is = 1<<interp_levels; // gone past end, so use last color if (h >= ramp_colors[numramp - 1].h) { return RGB8Color(ramp_colors[numramp-1].r, ramp_colors[numramp-1].g, ramp_colors[numramp-1].b); } for (int i = numramp - 2; i >= 0; i--) { assert(ramp_colors[i].h < ramp_colors[i + 1].h); if (h >= ramp_colors[i].h) { if (interp_levels) { f = (unsigned short)(h - ramp_colors[i].h) * is / (unsigned short)(ramp_colors[i + 1].h - ramp_colors[i].h); of = is - f; return RGB8Color((f * ramp_colors[i + 1].r + of * ramp_colors[i].r) >> interp_levels, (f * ramp_colors[i + 1].g + of * ramp_colors[i].g) >> interp_levels, (f * ramp_colors[i + 1].b + of * ramp_colors[i].b) >> interp_levels); } else { return RGB8Color(ramp_colors[i].r, ramp_colors[i].g, ramp_colors[i].b); } } }
bool ParseHexColor(const TCHAR *buffer, RGB8Color &color) { if (*buffer != _T('#')) return false; buffer++; TCHAR *endptr; unsigned value = ParseUnsigned(buffer, &endptr, 16); if (endptr != buffer + 6) return false; uint8_t r = value >> 16; uint8_t g = value >> 8; uint8_t b = value; color = RGB8Color(r, g, b); return true; }
constexpr RGB8Color Invert() const { return RGB8Color(~r, ~g, ~b); }
RGB8Color ColorRamp::ToRGB8Color() const { return RGB8Color(r, g, b); }