skia2/tests/HSVRoundTripTest.cpp
Leon Scroggins III 1252ec4bda Make SkColorToHSV and SkHSVToColor "perfect" inverses
For all possible opaque SkColors, make converting to HSV and back return
the original SkColor.

In SkHSVToColor, store values as normalized floats (instead of
converting to byte values) as long as possible.

Add a test that cycles through all opaque SkColors and verifies correct
conversion.

BUG=b/33737498

Change-Id: I7ff61a999a271565a9ffe82ae3c9676fc49d67e3
Reviewed-on: https://skia-review.googlesource.com/6720
Commit-Queue: Leon Scroggins <scroggo@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2017-01-11 18:12:36 +00:00

31 lines
938 B
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkColor.h"
DEF_TEST(ColorToHSVRoundTrip, reporter) {
SkScalar hsv[3];
for (U8CPU r = 0; r <= 255; r++) {
for (U8CPU g = 0; g <= 255; g++) {
for (U8CPU b = 0; b <= 255; b++) {
SkColor color = SkColorSetARGBInline(0xFF, r, g, b);
SkColorToHSV(color, hsv);
SkColor result = SkHSVToColor(0xFF, hsv);
if (result != color) {
ERRORF(reporter, "HSV roundtrip mismatch!\n"
"\toriginal: %X\n"
"\tHSV: %f, %f, %f\n"
"\tresult: %X\n",
color, hsv[0], hsv[1], hsv[2], result);
}
}
}
}
}