QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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:
33
35 template<typename T>
36 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
58 //draw border
59 p.setPen( QColor( 197, 197, 197 ) );
60 p.drawRect( margin, margin, width, height );
61 p.end();
62
63 QByteArray data;
64 QBuffer buffer( &data );
65 icon.save( &buffer, "PNG", 100 );
66
67 QString info = QStringLiteral( "<b>HEX</b> %1<br>" ).arg( color.name() );
68
69 if ( color.spec() == QColor::Spec::Cmyk )
70 {
71 const double cyan = color.cyanF() * 100.;
72 const double magenta = color.magentaF() * 100.;
73 const double yellow = color.yellowF() * 100.;
74 const double black = color.blackF() * 100.;
75 const double alpha = color.alphaF() * 100.;
76
77 info += QStringLiteral( "<b>CMYKA</b> %1,%2,%3,%4,%5<p>" )
78 .arg( cyan, 0, 'f', 2 ).arg( magenta, 0, 'f', 2 )
79 .arg( yellow, 0, 'f', 2 ).arg( black, 0, 'f', 2 )
80 .arg( alpha, 0, 'f', 2 );
81 }
82 else
83 {
84 const int hue = color.hue();
85 const int value = color.value();
86 const int saturation = color.saturation();
87
88 info += QStringLiteral( "<b>RGBA</b> %1<br>"
89 "<b>HSV</b> %2,%3,%4<p>" ).arg( QgsSymbolLayerUtils::encodeColor( color ) )
90 .arg( hue ).arg( saturation ).arg( value );
91 }
92
93 info += QStringLiteral( "<img src='data:image/png;base64, %1'>" ).arg( QString( data.toBase64() ) );
94
95 return info;
96 }
97};
98
99#endif
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:5667
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)