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