From 07599d0bc43d805b8bf9a6a21a0315ef9b2699f7 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Fri, 11 Apr 2014 14:40:12 +0400 Subject: [PATCH] color transform --- src/dlangui/graphics/drawbuf.d | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/dlangui/graphics/drawbuf.d b/src/dlangui/graphics/drawbuf.d index 593ef533..0d5a0718 100644 --- a/src/dlangui/graphics/drawbuf.d +++ b/src/dlangui/graphics/drawbuf.d @@ -3,6 +3,9 @@ module dlangui.graphics.drawbuf; public import dlangui.core.types; import dlangui.core.logger; +immutable uint COLOR_TRANSFORM_OFFSET_NONE = 0x80808080; +immutable uint COLOR_TRANSFORM_MULTIPLY_NONE = 0x80808080; + /// blend two RGB pixels using alpha uint blendARGB(uint dst, uint src, uint alpha) { uint dstalpha = dst >> 24; @@ -28,6 +31,26 @@ ubyte rgbToGray(uint color) { return cast(uint)(((srcr + srcg + srcg + srcb) >> 2) & 0xFF); } +uint transformComponent(int src, int addBefore, int multiply, int addAfter) { + int add1 = (cast(int)(addBefore << 1)) - 0x100; + int add2 = (cast(int)(addAfter << 1)) - 0x100; + int mul = cast(int)(multiply << 2); + int res = (((src + add1) * mul) >> 8) + add2; + if (res < 0) + res = 0; + else if (res > 255) + res = 255; + return cast(uint)res; +} + +uint transformRGBA(uint src, uint addBefore, uint multiply, uint addAfter) { + uint a = transformComponent(src >> 24, addBefore >> 24, multiply >> 24, addAfter >> 24); + uint r = transformComponent((src >> 16) & 0xFF, (addBefore >> 16) & 0xFF, (multiply >> 16) & 0xFF, (addAfter >> 16) & 0xFF); + uint g = transformComponent((src >> 8) & 0xFF, (addBefore >> 8) & 0xFF, (multiply >> 8) & 0xFF, (addAfter >> 8) & 0xFF); + uint b = transformComponent(src & 0xFF, addBefore & 0xFF, multiply & 0xFF, addAfter & 0xFF); + return (a << 24) | (r << 16) | (g << 8) | b; +} + /// blend two RGB pixels using alpha ubyte blendGray(ubyte dst, ubyte src, uint alpha) { uint ialpha = 256 - alpha; @@ -237,6 +260,12 @@ class DrawBuf : RefCountedObject { void drawImage(int x, int y, DrawBuf src) { drawFragment(x, y, src, Rect(0, 0, src.width, src.height)); } + + /// create drawbuf with copy of current buffer with changed colors (returns this if not supported) + DrawBuf transformColors(uint addBefore, uint multiply, uint addAfter) { + return this; + } + void clear() {} ~this() { clear(); } } @@ -677,6 +706,18 @@ class ColorDrawBuf : ColorDrawBufBase { for (int i = 0; i < len; i++) p[i] = color; } + override DrawBuf transformColors(uint addBefore, uint multiply, uint addAfter) { + if (addBefore == COLOR_TRANSFORM_OFFSET_NONE && addAfter == COLOR_TRANSFORM_OFFSET_NONE && multiply == COLOR_TRANSFORM_MULTIPLY_NONE) + return this; + ColorDrawBuf res = new ColorDrawBuf(_dx, _dy); + for (int y = 0; y < _dy; y++) { + uint * srcline = scanLine(y); + uint * dstline = res.scanLine(y); + for (int x = 0; x < _dx; x++) + dstline[x] = transformRGBA(srcline[x], addBefore, multiply, addAfter); + } + return res; + } }