ColorPrivTest.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkColor.h"
  8. #include "include/core/SkColorPriv.h"
  9. #include "include/core/SkTypes.h"
  10. #include "include/private/SkColorData.h"
  11. #include "tests/Test.h"
  12. #define ASSERT(expr) REPORTER_ASSERT(r, expr)
  13. DEF_TEST(Splay, r) {
  14. const SkPMColor color = 0xA1B2C3D4;
  15. uint32_t ag, rb;
  16. SkSplay(color, &ag, &rb);
  17. ASSERT(ag == 0x00A100C3);
  18. ASSERT(rb == 0x00B200D4);
  19. ASSERT(SkUnsplay(ag << 8, rb << 8) == color);
  20. const uint64_t agrb = SkSplay(color);
  21. ASSERT(agrb == 0x00A100C300B200D4ULL);
  22. ASSERT(SkUnsplay(agrb<<8) == color);
  23. }
  24. DEF_TEST(FourByteInterp, r) {
  25. const SkPMColor src = 0xAB998877, dst = 0x66334455;
  26. for (unsigned scale = 0; scale <= 256; scale++) {
  27. ASSERT(SkFourByteInterp256(src, dst, scale) == SkFastFourByteInterp256(src, dst, scale));
  28. }
  29. for (unsigned scale = 0; scale < 256; scale++) {
  30. // SkFourByteInterp and SkFastFourByteInterp convert from [0, 255] to [0, 256] differently.
  31. // In particular, slow may end up a little too high (weirdly, fast is more accurate).
  32. const SkPMColor slow = SkFourByteInterp(src, dst, scale);
  33. const SkPMColor fast = SkFastFourByteInterp(src, dst, scale);
  34. const int deltaA = SkGetPackedA32(slow) - SkGetPackedA32(fast);
  35. const int deltaR = SkGetPackedR32(slow) - SkGetPackedR32(fast);
  36. const int deltaG = SkGetPackedG32(slow) - SkGetPackedG32(fast);
  37. const int deltaB = SkGetPackedB32(slow) - SkGetPackedB32(fast);
  38. ASSERT(deltaA == 0 || deltaA == 1);
  39. ASSERT(deltaR == 0 || deltaR == 1);
  40. ASSERT(deltaG == 0 || deltaG == 1);
  41. ASSERT(deltaB == 0 || deltaB == 1);
  42. }
  43. }