File indexing completed on 2024-10-13 07:24:06
0001 /* 0002 SPDX-FileCopyrightText: 2010 Till Theato <root@ttill.de> 0003 SPDX-FileCopyrightText: 2017 Dušan Hanuš <hanus@pixelhouse.cz> 0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "qcolorutils.h" 0008 0009 #include <QTextStream> 0010 0011 QColor QColorUtils::stringToColor(QString strColor) 0012 { 0013 bool ok = false; 0014 QColor color("black"); 0015 if (strColor.startsWith(QLatin1String("0x"))) { 0016 if (strColor.length() == 10) { 0017 // 0xRRGGBBAA 0018 uint intval = strColor.toUInt(&ok, 16); 0019 color.setRgb((intval >> 24) & 0xff, // r 0020 (intval >> 16) & 0xff, // g 0021 (intval >> 8) & 0xff, // b 0022 intval & 0xff); // a 0023 } else { 0024 // 0xRRGGBB, 0xRGB 0025 color.setNamedColor(strColor.replace(0, 2, QLatin1Char('#'))); 0026 } 0027 } else { 0028 if (strColor.length() == 9) { 0029 // #AARRGGBB 0030 strColor = strColor.replace('#', QLatin1String("0x")); 0031 uint intval = strColor.toUInt(&ok, 16); 0032 color.setRgb((intval >> 16) & 0xff, // r 0033 (intval >> 8) & 0xff, // g 0034 intval & 0xff, // b 0035 (intval >> 24) & 0xff); // a 0036 } else if (strColor.length() == 8) { 0037 // 0xRRGGBB 0038 strColor = strColor.replace('#', QLatin1String("0x")); 0039 color.setNamedColor(strColor); 0040 } else { 0041 // #RRGGBB, #RGB 0042 color.setNamedColor(strColor); 0043 } 0044 } 0045 0046 return color; 0047 } 0048 0049 QString QColorUtils::colorToString(const QColor &color, bool alpha) 0050 { 0051 QString colorStr; 0052 QTextStream stream(&colorStr); 0053 stream << "0x"; 0054 stream.setIntegerBase(16); 0055 stream.setFieldWidth(2); 0056 stream.setFieldAlignment(QTextStream::AlignRight); 0057 stream.setPadChar('0'); 0058 stream << color.red() << color.green() << color.blue(); 0059 if (alpha) { 0060 stream << color.alpha(); 0061 } else { 0062 // MLT always wants 0xRRGGBBAA format 0063 stream << "ff"; 0064 } 0065 return colorStr; 0066 } 0067 0068 QColor QColorUtils::complementary(QColor color) 0069 { 0070 color = color.toHsv(); 0071 int hue = color.hsvHue() + 180; 0072 if (hue > 359) { 0073 hue -= 360; 0074 } 0075 color.setHsv(hue, color.hsvSaturation(), color.value()); 0076 return color; 0077 } 0078 0079 NegQColor::NegQColor() {} 0080 0081 NegQColor NegQColor::fromHsvF(qreal h, qreal s, qreal l, qreal a) 0082 { 0083 NegQColor color; 0084 color.qcolor = QColor::fromHsvF(qBound(0., h, 1.), qBound(0., s, 1.), qBound(0., (l < 0 ? -l : l), 1.), a); 0085 color.sign_r = l < 0 ? -1 : 1; 0086 color.sign_g = l < 0 ? -1 : 1; 0087 color.sign_b = l < 0 ? -1 : 1; 0088 return color; 0089 } 0090 0091 QDebug operator<<(QDebug qd, const NegQColor &c) 0092 { 0093 qd << c.qcolor << "(redF" << c.redF() << "greenF" << c.greenF() << "blueF" << c.blueF() << "valueF" << c.valueF() << "hueF" << c.hueF() << ")"; 0094 return qd.maybeSpace(); 0095 } 0096 0097 bool NegQColor::operator==(const NegQColor &other) const 0098 { 0099 return other.redF() == redF() && other.greenF() == greenF() && other.blueF() == blueF() && other.hueF() == hueF(); 0100 } 0101 0102 bool NegQColor::operator!=(const NegQColor &other) const 0103 { 0104 return other.redF() != redF() || other.greenF() != greenF() || other.blueF() != blueF() || other.hueF() != hueF(); 0105 } 0106 0107 NegQColor NegQColor::fromRgbF(qreal r, qreal g, qreal b, qreal a) 0108 { 0109 NegQColor color; 0110 color.qcolor = QColor::fromRgbF(r < 0 ? -r : r, g < 0 ? -g : g, b < 0 ? -b : b, a); 0111 color.sign_r = r < 0 ? -1 : 1; 0112 color.sign_g = g < 0 ? -1 : 1; 0113 color.sign_b = b < 0 ? -1 : 1; 0114 return color; 0115 } 0116 0117 qreal NegQColor::redF() const 0118 { 0119 return qcolor.redF() * sign_r; 0120 } 0121 0122 void NegQColor::setRedF(qreal val) 0123 { 0124 sign_r = val < 0 ? -1 : 1; 0125 qcolor.setRedF(val * sign_r); 0126 } 0127 0128 qreal NegQColor::greenF() const 0129 { 0130 return qcolor.greenF() * sign_g; 0131 } 0132 0133 void NegQColor::setGreenF(qreal val) 0134 { 0135 sign_g = val < 0 ? -1 : 1; 0136 qcolor.setGreenF(val * sign_g); 0137 } 0138 0139 qreal NegQColor::blueF() const 0140 { 0141 return qcolor.blueF() * sign_b; 0142 } 0143 0144 void NegQColor::setBlueF(qreal val) 0145 { 0146 sign_b = val < 0 ? -1 : 1; 0147 qcolor.setBlueF(val * sign_b); 0148 } 0149 0150 void NegQColor::setValueF(qreal val) 0151 { 0152 qcolor = QColor::fromHsvF(hueF(), saturationF(), qBound(0., (val < 0 ? -val : val), 1.), 1.); 0153 sign_r = val < 0 ? -1 : 1; 0154 sign_g = val < 0 ? -1 : 1; 0155 sign_b = val < 0 ? -1 : 1; 0156 } 0157 0158 qreal NegQColor::valueF() const 0159 { 0160 return qcolor.valueF() * sign_g; 0161 } 0162 0163 int NegQColor::hue() const 0164 { 0165 return qcolor.hue(); 0166 } 0167 0168 qreal NegQColor::hueF() const 0169 { 0170 return qcolor.hueF(); 0171 } 0172 0173 qreal NegQColor::saturationF() const 0174 { 0175 return qcolor.saturationF(); 0176 }