Skip to content

Commit

Permalink
feat(CMYK): Add a specific preview for CMYK color
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Aug 21, 2024
1 parent bcdf85a commit 3cabec5
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 76 deletions.
44 changes: 3 additions & 41 deletions src/gui/qgscolorbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "qgscolorbutton.h"
#include "qgscolordialog.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgssymbollayerutils.h"
#include "qgscolorswatchgrid.h"
#include "qgscolorschemeregistry.h"
Expand All @@ -25,6 +24,7 @@
#include "qgsproject.h"
#include "qgsguiutils.h"
#include "qgsgui.h"
#include "qgscolortooltip_p.h"

#include <QPainter>
#include <QMouseEvent>
Expand Down Expand Up @@ -175,47 +175,10 @@ bool QgsColorButton::event( QEvent *e )
if ( !isProjectColor )
c = mColor;

const QString name = c.name();
const int hue = c.hue();
const int value = c.value();
const int saturation = c.saturation();
QString info = ( isProjectColor ? QStringLiteral( "<p>%1: %2</p>" ).arg( tr( "Linked color" ), mLinkedColorName ) : QString() );

// create very large preview swatch
const int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 23 );
const int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
info += QgsColorTooltip::htmlDescription( c, this );

const int margin = static_cast< int >( height * 0.1 );
QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
icon.fill( Qt::transparent );

QPainter p;
p.begin( &icon );

//start with checkboard pattern
const QBrush checkBrush = QBrush( transparentBackground() );
p.setPen( Qt::NoPen );
p.setBrush( checkBrush );
p.drawRect( margin, margin, width, height );

//draw color over pattern
p.setBrush( QBrush( c ) );

//draw border
p.setPen( QColor( 197, 197, 197 ) );
p.drawRect( margin, margin, width, height );
p.end();

QByteArray data;
QBuffer buffer( &data );
icon.save( &buffer, "PNG", 100 );

const QString info = ( isProjectColor ? QStringLiteral( "<p>%1: %2</p>" ).arg( tr( "Linked color" ), mLinkedColorName ) : QString() )
+ QStringLiteral( "<b>HEX</b> %1<br>"
"<b>RGB</b> %2<br>"
"<b>HSV</b> %3,%4,%5<p>"
"<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ), name,
QgsSymbolLayerUtils::encodeColor( c ) )
.arg( hue ).arg( saturation ).arg( value );
setToolTip( info );
}
return QToolButton::event( e );
Expand Down Expand Up @@ -862,4 +825,3 @@ void QgsColorButton::linkToProjectColor( const QString &name )
mLinkedColorName = name;
setButtonBackground();
}

2 changes: 2 additions & 0 deletions src/gui/qgscolorbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ class GUI_EXPORT QgsColorButton : public QToolButton
* Creates the drop-down menu entries
*/
void prepareMenu();

friend class QgsColorTooltip;
};

#endif
37 changes: 2 additions & 35 deletions src/gui/qgscolorswatchgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "qgscolorswatchgrid.h"
#include "qgsapplication.h"
#include "qgssymbollayerutils.h"
#include "qgslogger.h"
#include "qgscolortooltip_p.h"
#include <QPainter>
#include <QMouseEvent>
#include <QMenu>
Expand Down Expand Up @@ -121,44 +121,11 @@ void QgsColorSwatchGrid::updateTooltip( const int colorIdx )
//if color has an associated name from the color scheme, use that
const QString colorName = mColors.at( colorIdx ).second;

// create very large preview swatch, because the grid itself has only tiny preview icons
const int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 23 );
const int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
const int margin = static_cast< int >( height * 0.1 );
QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
icon.fill( Qt::transparent );

QPainter p;
p.begin( &icon );

//start with checkboard pattern
const QBrush checkBrush = QBrush( transparentBackground() );
p.setPen( Qt::NoPen );
p.setBrush( checkBrush );
p.drawRect( margin, margin, width, height );

//draw color over pattern
p.setBrush( QBrush( mColors.at( colorIdx ).first ) );

//draw border
p.setPen( QColor( 197, 197, 197 ) );
p.drawRect( margin, margin, width, height );
p.end();

QByteArray data;
QBuffer buffer( &data );
icon.save( &buffer, "PNG", 100 );

QString info;
if ( !colorName.isEmpty() )
info += QStringLiteral( "<h3>%1</h3><p>" ).arg( colorName );

info += QStringLiteral( "<b>HEX</b> %1<br>"
"<b>RGB</b> %2<br>"
"<b>HSV</b> %3,%4,%5<p>" ).arg( color.name(),
QgsSymbolLayerUtils::encodeColor( color ) )
.arg( color.hue() ).arg( color.saturation() ).arg( color.value() );
info += QStringLiteral( "<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ) );
info += QgsColorTooltip::htmlDescription( color, this );

setToolTip( info );

Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgscolorswatchgrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class GUI_EXPORT QgsColorSwatchGrid : public QWidget
* \returns checkboard pixmap
*/
QPixmap transparentBackground();

friend class QgsColorTooltip;
};


Expand Down
94 changes: 94 additions & 0 deletions src/gui/qgscolortooltip_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/***************************************************************************
qgscolortooltip_p.h
---------------------
begin : 2024/08/21
copyright : (C) 2024 by Julien Cabieces
email : julien dot cabieces at oslandia dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSCOLORTOOLTIP_P_H
#define QGSCOLORTOOLTIP_P_H

#include <QPainter>
#include <QBuffer>

#include "qgssymbollayerutils.h"

//! helper class to generate color tooltip
class QgsColorTooltip
{
public:

//! Returns an HTML desciption given a \a color with a preview image of the color
template<typename T>
static QString htmlDescription( QColor color, T *widget )
{
// create very large preview swatch
const int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * widget->fontMetrics().horizontalAdvance( 'X' ) * 23 );
const int height = static_cast< int >( width / 1.61803398875 ); // golden ratio

const int margin = static_cast< int >( height * 0.1 );
QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
icon.fill( Qt::transparent );

QPainter p;
p.begin( &icon );

//start with checkboard pattern
const QBrush checkBrush = QBrush( widget->transparentBackground() );
p.setPen( Qt::NoPen );
p.setBrush( checkBrush );
p.drawRect( margin, margin, width, height );

//draw color over pattern
p.setBrush( QBrush( color ) );

//draw border
p.setPen( QColor( 197, 197, 197 ) );
p.drawRect( margin, margin, width, height );
p.end();

QByteArray data;
QBuffer buffer( &data );
icon.save( &buffer, "PNG", 100 );

QString info = QStringLiteral( "<b>HEX</b> %1<br>" ).arg( color.name() );

if ( color.spec() == QColor::Spec::Cmyk )
{
const float cyan = color.cyanF() * 100.f;
const float magenta = color.magentaF() * 100.f;
const float yellow = color.yellowF() * 100.f;
const float black = color.blackF() * 100.f;
const float alpha = color.alphaF() * 100.f;

info += QStringLiteral( "<b>CMYKA</b> %1,%2,%3,%4,%5<p>" )
.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 );
}
else
{
const int hue = color.hue();
const int value = color.value();
const int saturation = color.saturation();

info += QStringLiteral( "<b>RGB</b> %1<br>"
"<b>HSV</b> %2,%3,%4<p>" ).arg( QgsSymbolLayerUtils::encodeColor( color ) )
.arg( hue ).arg( saturation ).arg( value );
}

info += QStringLiteral( "<img src='data:image/png;base64, %1'>" ).arg( QString( data.toBase64() ) );

return info;
}
};

#endif

0 comments on commit 3cabec5

Please sign in to comment.