Qt中QRgb与QColor的区别及使用
1、QRgb:
QRgb的官方文档如下:
An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
The type also holds a value for the alpha-channel. The default alpha channel is ff, i.e opaque. For more information, see the Alpha-Blended Drawing section.
也即是:QRgb是格式为#AARRGGBB的ARGB四元组,相当于无符号整数。该类型还保存alpha通道的值AA。默认alpha通道为ff,即不透明。(alpha通道见一下篇)
另外有:
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, r, g, b).
2、QColor
The QColor class provides colors based on RGB, HSV or CMYK values
可以参考QColor其中的两个构造函数:
也就是即可以通过QRgb构造,也可以通过给定r、g、b等的值构造。
3、相互转换
QColor mycolor(127,127,127);
QRgb myQrgb = qRgb(mycolor.red(),mycolor.green(),mycolor.blue());
QColor myQColor = QColor(myQrgb);
qDebug() << myQrgb << " " << myQColor << Qt::endl;
以上输出结果为:4286545791 QColor(ARGB 1, 0.498039, 0.498039, 0.498039)
注意到QRgb输出的结果为十进制的数字,而其对应的十六进制为:ff7f7f7f,正好是 #AARRGGBB的形式,而0.498039为 127 255 \frac{127}{255} 255127。
由以上可得,直接给QRgb赋一个int类型的数值也是可以的,例如:
QRgb mRgb = 4278453252; // 4278453252对应的十六进制为:ff03b5ee 而03的十进制为3 b5的十进制为181 ee的十进制为238
QColor mColor = QColor(mRgb);
qDebug() << mColor.red() << mColor.green() << mColor.blue() << Qt::endl;
上述程序结果为: 3 181 238