1 /**************************************************************************** 2 ** 3 ** DQt - D bindings for the Qt Toolkit 4 ** 5 ** GNU Lesser General Public License Usage 6 ** This file may be used under the terms of the GNU Lesser 7 ** General Public License version 3 as published by the Free Software 8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 9 ** packaging of this file. Please review the following information to 10 ** ensure the GNU Lesser General Public License version 3 requirements 11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 12 ** 13 ****************************************************************************/ 14 module qt.gui.rgb; 15 extern(C++): 16 17 import qt.config; 18 import qt.helpers; 19 20 alias QRgb = uint; // RGB triplet 21 22 // non-namespaced Qt global variable 23 /+ Q_DECL_UNUSED +/ __gshared const(QRgb) RGB_MASK = 0x00ffffff; // masks RGB values 24 25 pragma(inline, true) int qRed(QRgb rgb) // get red part of RGB 26 { return ((rgb >> 16) & 0xff); } 27 28 pragma(inline, true) int qGreen(QRgb rgb) // get green part of RGB 29 { return ((rgb >> 8) & 0xff); } 30 31 pragma(inline, true) int qBlue(QRgb rgb) // get blue part of RGB 32 { return (rgb & 0xff); } 33 34 pragma(inline, true) int qAlpha(QRgb rgb) // get alpha part of RGBA 35 { return rgb >> 24; } 36 37 pragma(inline, true) QRgb qRgb(int r, int g, int b)// set RGB value 38 { return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); } 39 40 pragma(inline, true) QRgb qRgba(int r, int g, int b, int a)// set RGBA value 41 { return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); } 42 43 pragma(inline, true) int qGray(int r, int g, int b)// convert R,G,B to gray 0..255 44 { return (r*11+g*16+b*5)/32; } 45 46 pragma(inline, true) int qGray(QRgb rgb) // convert RGB to gray 0..255 47 { return qGray(qRed(rgb), qGreen(rgb), qBlue(rgb)); } 48 49 pragma(inline, true) bool qIsGray(QRgb rgb) 50 { return qRed(rgb) == qGreen(rgb) && qRed(rgb) == qBlue(rgb); } 51 52 pragma(inline, true) QRgb qPremultiply(QRgb x) 53 { 54 const(uint) a = qAlpha(x); 55 uint t = (x & 0xff00ff) * a; 56 t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8; 57 t &= 0xff00ff; 58 59 x = ((x >> 8) & 0xff) * a; 60 x = (x + ((x >> 8) & 0xff) + 0x80); 61 x &= 0xff00; 62 return x | t | (a << 24); 63 } 64 65 mixin(mangleWindows("?qt_inv_premul_factor@@3QBIB", q{ 66 /+ Q_GUI_EXPORT +/ extern export __gshared const(uint)[0] qt_inv_premul_factor; 67 })); 68 69 pragma(inline, true) QRgb qUnpremultiply(QRgb p) 70 { 71 const(uint) alpha = qAlpha(p); 72 // Alpha 255 and 0 are the two most common values, which makes them beneficial to short-cut. 73 if (alpha == 255) 74 return p; 75 if (alpha == 0) 76 return 0; 77 // (p*(0x00ff00ff/alpha)) >> 16 == (p*255)/alpha for all p and alpha <= 256. 78 const(uint) invAlpha = qt_inv_premul_factor. ptr[alpha]; 79 // We add 0x8000 to get even rounding. The rounding also ensures that qPremultiply(qUnpremultiply(p)) == p for all p. 80 return qRgba((qRed(p)*invAlpha + 0x8000)>>16, (qGreen(p)*invAlpha + 0x8000)>>16, (qBlue(p)*invAlpha + 0x8000)>>16, alpha); 81 } 82