diff options
author | brianosman <brianosman@google.com> | 2016-06-24 12:50:19 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-06-24 12:50:19 -0700 |
commit | a4535a34d1b317543307df6901debfefe7132569 (patch) | |
tree | c15e99990ff4bccd09e56e35ccf8441d55e0c83e /include | |
parent | e2cddc5342d5aacacce4429441c94d1e0ff67d8c (diff) |
GrColor4f is yet another 4f color type, unfortunately.
- Sk4f would be my choice, but it's not allowed in include/
- SkColor4f and SkPM4f are specified to be unpremultiplied/premultiplied, whereas GrColor (and GrColor4f) are either, depending on context.
This adds 12 bytes to GrPaint. Not sure if we want to pay that price. The precision loss for a single value (vs. in a gradient, etc...) may not justify changing the storage type here. Easy enough to back that part out, while still keeping the 4f intermediate type for the helper math that it adds, and for storage and parameter passing in other locations.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2088303002
Review-Url: https://codereview.chromium.org/2088303002
Diffstat (limited to 'include')
-rw-r--r-- | include/gpu/GrColor.h | 48 | ||||
-rw-r--r-- | include/gpu/GrPaint.h | 11 |
2 files changed, 56 insertions, 3 deletions
diff --git a/include/gpu/GrColor.h b/include/gpu/GrColor.h index 77f1a6cb08..911b18fc16 100644 --- a/include/gpu/GrColor.h +++ b/include/gpu/GrColor.h @@ -169,6 +169,54 @@ static inline GrColor GrUnpremulColor(GrColor color) { return GrColorPackRGBA(r, g, b, a); } + +/** +* Similarly, GrColor4f is 4 floats for R, G, B, A, in that order. And like GrColor, whether +* the color is premultiplied or not depends on the context. +*/ +struct GrColor4f { + float fRGBA[4]; + + GrColor4f() {} + GrColor4f(float r, float g, float b, float a) { + fRGBA[0] = r; + fRGBA[1] = g; + fRGBA[2] = b; + fRGBA[3] = a; + } + + static GrColor4f FromGrColor(GrColor color) { + GrColor4f result; + GrColorToRGBAFloat(color, result.fRGBA); + return result; + } + + static GrColor4f FromSkColor4f(const SkColor4f& color) { + return GrColor4f(color.fR, color.fG, color.fB, color.fA); + } + + GrColor toGrColor() const { + return GrColorPackRGBA( + SkTPin<unsigned>(static_cast<unsigned>(fRGBA[0] * 255.0f + 0.5f), 0, 255), + SkTPin<unsigned>(static_cast<unsigned>(fRGBA[1] * 255.0f + 0.5f), 0, 255), + SkTPin<unsigned>(static_cast<unsigned>(fRGBA[2] * 255.0f + 0.5f), 0, 255), + SkTPin<unsigned>(static_cast<unsigned>(fRGBA[3] * 255.0f + 0.5f), 0, 255)); + } + + SkColor4f toSkColor4f() const { + return SkColor4f { fRGBA[0], fRGBA[1], fRGBA[2], fRGBA[3] }; + } + + GrColor4f opaque() const { + return GrColor4f(fRGBA[0], fRGBA[1], fRGBA[2], 1.0f); + } + + GrColor4f premul() const { + float a = fRGBA[3]; + return GrColor4f(fRGBA[0] * a, fRGBA[1] * a, fRGBA[2] * a, a); + } +}; + /** * Flags used for bitfields of color components. They are defined so that the bit order reflects the * GrColor shift order. diff --git a/include/gpu/GrPaint.h b/include/gpu/GrPaint.h index af408488d7..70067ae449 100644 --- a/include/gpu/GrPaint.h +++ b/include/gpu/GrPaint.h @@ -48,8 +48,13 @@ public: /** * The initial color of the drawn primitive. Defaults to solid white. */ - void setColor(GrColor color) { fColor = color; } - GrColor getColor() const { return fColor; } + void setColor4f(const GrColor4f& color) { fColor = color; } + const GrColor4f& getColor4f() const { return fColor; } + + /** + * Legacy getter, until all code handles 4f directly. + */ + GrColor getColor() const { return fColor.toGrColor(); } /** * Should primitives be anti-aliased or not. Defaults to false. @@ -162,7 +167,7 @@ private: bool fDisableOutputConversionToSRGB; bool fAllowSRGBInputs; - GrColor fColor; + GrColor4f fColor; }; #endif |