libcruft-util/test/colour.cpp

50 lines
1.6 KiB
C++
Raw Normal View History

#include "colour.hpp"
2015-04-13 18:06:08 +10:00
2015-04-13 16:45:56 +10:00
#include "tap.hpp"
2016-07-28 13:31:52 +10:00
int
main (int, char**)
{
2015-04-13 16:45:56 +10:00
util::TAP::logger tap;
2015-04-09 20:45:55 +10:00
// Check casting works between intergral and floating formats
{
util::colour4f f (1);
util::colour<4,uint8_t> u (255);
2015-04-13 16:45:56 +10:00
tap.expect_eq (f.cast<uint8_t> (), u, "cast float to u8");
tap.expect_eq (u.cast<float> (), f, "cast u8 to float");
2015-04-09 20:45:55 +10:00
}
2015-04-09 20:46:55 +10:00
// Check lookups are working
2016-12-12 17:03:36 +11:00
tap.expect_eq (util::colour4f::from_html ("white"), util::colour4f {1}, "HTML lookup");
tap.expect_eq ( util::colour4f::from_x11 ("white"), util::colour4f {1}, "X11 lookup");
2015-04-09 21:50:42 +10:00
// Check HSV conversions
{
// white: hue is undefined
auto white = util::rgb_to_hsv ({1,1,1});
tap.expect (util::exactly_zero (white.s) && util::exactly_equal (white.v, 1), "white hsv");
2015-04-09 21:50:42 +10:00
// black: hue is undefined
auto black = util::rgb_to_hsv ({0,0,0});
tap.expect (util::exactly_zero (black.s) && util::exactly_zero (black.v), "black hsv");
2015-04-09 21:50:42 +10:00
struct {
2015-04-13 16:45:56 +10:00
const char *name;
2015-04-09 21:50:42 +10:00
util::colour3f rgb;
util::colour3f hsv;
} TESTS[] = {
2015-04-13 16:45:56 +10:00
{ "red", { 1, 0, 0, }, { 0, 1, 1, } }, // red
{ "green", { 0, 1, 0, }, { 120, 1, 1, } }, // green
{ "blue", { 0, 0, 1, }, { 240, 1, 1, } }, // blue
{ "misc", { 0.75f, 0.25f, 0.75f }, { 300, 2/3.f, 0.75f } },
2015-04-09 21:50:42 +10:00
};
for (auto i: TESTS) {
tap.expect_eq (util::rgb_to_hsv (i.rgb), i.hsv, "rgb-to-hsv %s", i.name);
tap.expect_eq (util::hsv_to_rgb (i.hsv), i.rgb, "hsv-to-rgb %s", i.name);
2015-04-09 21:50:42 +10:00
}
}
}