QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgscolortooltip_p.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscolortooltip_p.h
3 ---------------------
4 begin : 2024/08/21
5 copyright : (C) 2024 by Julien Cabieces
6 email : julien dot cabieces at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSCOLORTOOLTIP_P_H
17#define QGSCOLORTOOLTIP_P_H
18
19#include "qgssymbollayerutils.h"
20
21#include <QBuffer>
22#include <QPainter>
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
33{
34 public:
36 template<typename T>
37 static QString htmlDescription( QColor color, T *widget )
38 {
39 // create very large preview swatch
40 const int width = static_cast<int>( Qgis::UI_SCALE_FACTOR * widget->fontMetrics().horizontalAdvance( 'X' ) * 23 );
41 const int height = static_cast<int>( width / 1.61803398875 ); // golden ratio
42
43 const int margin = static_cast<int>( height * 0.1 );
44 QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
45 icon.fill( Qt::transparent );
46
47 QPainter p;
48 p.begin( &icon );
49
50 //start with checkboard pattern
51 const QBrush checkBrush = QBrush( widget->transparentBackground() );
52 p.setPen( Qt::NoPen );
53 p.setBrush( checkBrush );
54 p.drawRect( margin, margin, width, height );
55
56 //draw color over pattern
57 p.setBrush( QBrush( color ) );
58 p.drawRect( margin, margin, width, height );
59
60 if ( color.alpha() < 255 )
61 {
62 //draw fully opaque color over half of the area
63 color.setAlpha( 255 );
64 p.setBrush( QBrush( color ) );
65 p.drawRect( margin, margin, width / 2, height );
66 }
67
68 //draw border
69 p.setPen( QColor( 197, 197, 197 ) );
70 p.setBrush( Qt::NoBrush );
71 p.drawRect( margin, margin, width, height );
72 p.end();
73
74 QByteArray data;
75 QBuffer buffer( &data );
76 icon.save( &buffer, "PNG", 100 );
77
78 QString info = u"<b>HEX</b> %1<br>"_s.arg( color.name() );
79
80 if ( color.spec() == QColor::Spec::Cmyk )
81 {
82 const double cyan = color.cyanF() * 100.;
83 const double magenta = color.magentaF() * 100.;
84 const double yellow = color.yellowF() * 100.;
85 const double black = color.blackF() * 100.;
86 const double alpha = color.alphaF() * 100.;
87
88 info += u"<b>CMYKA</b> %1,%2,%3,%4,%5<p>"_s
89 .arg( cyan, 0, 'f', 2 )
90 .arg( magenta, 0, 'f', 2 )
91 .arg( yellow, 0, 'f', 2 )
92 .arg( black, 0, 'f', 2 )
93 .arg( alpha, 0, 'f', 2 );
94 }
95 else
96 {
97 const int hue = color.hue();
98 const int value = color.value();
99 const int saturation = color.saturation();
100
101 info += QStringLiteral( "<b>RGBA</b> %1<br>"
102 "<b>HSV</b> %2,%3,%4<p>" )
103 .arg( QgsSymbolLayerUtils::encodeColor( color ) )
104 .arg( hue )
105 .arg( saturation )
106 .arg( value );
107 }
108
109 info += u"<img src='data:image/png;base64, %1'>"_s.arg( QString( data.toBase64() ) );
110
111 return info;
112 }
113};
114
115#endif
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:6534
Helper class for generating color tooltips.
static QString htmlDescription(QColor color, T *widget)
Returns an HTML description given a color with a preview image of the color.
static QString encodeColor(const QColor &color)