QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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> static QString htmlDescription( QColor color, T *widget )
37 {
38 // create very large preview swatch
39 const int width = static_cast<int>( Qgis::UI_SCALE_FACTOR * widget->fontMetrics().horizontalAdvance( 'X' ) * 23 );
40 const int height = static_cast<int>( width / 1.61803398875 ); // golden ratio
41
42 const int margin = static_cast<int>( height * 0.1 );
43 QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
44 icon.fill( Qt::transparent );
45
46 QPainter p;
47 p.begin( &icon );
48
49 //start with checkboard pattern
50 const QBrush checkBrush = QBrush( widget->transparentBackground() );
51 p.setPen( Qt::NoPen );
52 p.setBrush( checkBrush );
53 p.drawRect( margin, margin, width, height );
54
55 //draw color over pattern
56 p.setBrush( QBrush( color ) );
57 p.drawRect( margin, margin, width, height );
58
59 if ( color.alpha() < 255 )
60 {
61 //draw fully opaque color over half of the area
62 color.setAlpha( 255 );
63 p.setBrush( QBrush( color ) );
64 p.drawRect( margin, margin, width / 2, height );
65 }
66
67 //draw border
68 p.setPen( QColor( 197, 197, 197 ) );
69 p.setBrush( Qt::NoBrush );
70 p.drawRect( margin, margin, width, height );
71 p.end();
72
73 QByteArray data;
74 QBuffer buffer( &data );
75 icon.save( &buffer, "PNG", 100 );
76
77 QString info = u"<b>HEX</b> %1<br>"_s.arg( color.name() );
78
79 if ( color.spec() == QColor::Spec::Cmyk )
80 {
81 const double cyan = color.cyanF() * 100.;
82 const double magenta = color.magentaF() * 100.;
83 const double yellow = color.yellowF() * 100.;
84 const double black = color.blackF() * 100.;
85 const double alpha = color.alphaF() * 100.;
86
87 info += u"<b>CMYKA</b> %1,%2,%3,%4,%5<p>"_s.arg( cyan, 0, 'f', 2 ).arg( magenta, 0, 'f', 2 ).arg( yellow, 0, 'f', 2 ).arg( black, 0, 'f', 2 ).arg( alpha, 0, 'f', 2 );
88 }
89 else
90 {
91 const int hue = color.hue();
92 const int value = color.value();
93 const int saturation = color.saturation();
94
95 info += QStringLiteral(
96 "<b>RGBA</b> %1<br>"
97 "<b>HSV</b> %2,%3,%4<p>"
98 )
100 .arg( hue )
101 .arg( saturation )
102 .arg( value );
103 }
104
105 info += u"<img src='data:image/png;base64, %1'>"_s.arg( QString( data.toBase64() ) );
106
107 return info;
108 }
109};
110
111#endif
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:6591
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)