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