QIcon RColor::getIcon(const RColor& color, const QSize& size) { init(); if (iconMap.contains(QPair<RColor, QPair<int, int> >(color, QPair<int, int>(size.width(), size.height())))) { return iconMap[QPair<RColor, QPair<int, int> >(color, QPair<int, int>(size.width(), size.height()))]; } RColor col = color; if (color.isByLayer() || color.isByBlock() || /*color == RColor(Qt::black, RColor::BlackWhite) ||*/ !color.isValid()) { col = Qt::white; } QImage img(size, QImage::Format_ARGB32_Premultiplied); img.fill(0); QPainter painter(&img); int w = img.width(); int h = img.height(); // painter.setCompositionMode(QPainter::CompositionMode_Source); painter.fillRect(0, 0, w, h, col); if (!color.isValid()) { // icon for "Other colors..." QLinearGradient grad(0, 0, w, 0); grad.setColorAt(0, Qt::red); grad.setColorAt(0.33, Qt::yellow); grad.setColorAt(0.66, Qt::blue); grad.setColorAt(1, Qt::green); painter.fillRect(QRect(0, 0, w, h), grad); } /*else if (color == RColor(Qt::black, RColor::BlackWhite) || color == RColor(Qt::white, RColor::BlackWhite)) { // icon for black / white painter.setRenderHint(QPainter::Antialiasing); QPainterPath path; path.moveTo(0, 0); path.lineTo(w, 0); path.lineTo(w, h); painter.fillPath(path, Qt::black); painter.setRenderHint(QPainter::Antialiasing, false); }*/ else if (col.alpha() != 255) { // indicate alpha by an inset QBrush opaqueBrush = col; col.setAlpha(255); opaqueBrush.setColor(col); painter.fillRect(w / 4, h / 4, w / 2, h / 2, opaqueBrush); } painter.setPen(Qt::black); painter.drawRect(0, 0, w - 1, h - 1); painter.end(); QIcon ret(QPixmap::fromImage(img)); iconMap.insert(QPair<RColor, QPair<int, int> >(color, QPair<int, int>(size.width(), size.height())), ret); return ret; }
/** * Stream operator for QDebug */ QDebug operator<<(QDebug dbg, const RColor& c) { if (c.isValid()) { if (c.isByLayer()) { dbg.nospace() << "RColor(ByLayer)"; } else if (c.isByBlock()) { dbg.nospace() << "RColor(ByBlock)"; } else { dbg.nospace() << "RColor(RGBA: " << c.red() << ", " << c.green() << ", " << c.blue() << ", " << c.alpha() << ")"; } } else { dbg.nospace() << "RColor(invalid)"; } return dbg.space(); }
/** * \return Highlighted color for the given color. */ RColor RColor::getHighlighted(const RColor& color, const QColor& bgColor, int minDist) { if (!color.isValid()) { return Qt::gray; } RColor ret = color; int vColor = color.value(); int vBgColor = bgColor.value(); // 0 vColor vBgColor 255 // |--------^----------------------------^------------------| // |<--d1-->|<------------d2------------>|<-------d3------->| int d1 = qMin(vColor, vBgColor); //int d2 = qAbs(vColor - vBgColor); int d3 = 255 - qMax(vColor, vBgColor); // d3 is big enough: set value to max (highlight): if (d3>=minDist) { ret.setHsv(color.hue(), color.saturation(), 255); } // d1 is big enough: set value to half (lowlight): else if (d1>=minDist) { ret.setHsv(color.hue(), color.saturation(), qMin(vColor, vBgColor)/2); } // black on white: else if (vColor<32 && vBgColor>224) { ret.setHsv(color.hue(), color.saturation(), 160); } // d2 is the only significant distance, set value to medium: else if (vColor<vBgColor) { ret.setHsv(color.hue(), color.saturation(), qMin(vColor+minDist, 255)); } else { ret.setHsv(color.hue(), color.saturation(), qMax(vColor-minDist, 0)); } return ret; }