QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgscolorrampimpl.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorrampimpl.h
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail 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 QGSCOLORRAMPIMPL_H
17#define QGSCOLORRAMPIMPL_H
18
19#include "qgis.h"
20#include "qgis_core.h"
21#include "qgscolorramp.h"
22#include "qgscolorscheme.h"
23
24#include <QColor>
25#include <QGradient>
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
30#ifndef SIP_RUN
32typedef QColor ( *InterpolateColorFunc )( const QColor &c1, const QColor &c2, const double value, Qgis::AngularDirection direction );
34#endif
35
41class CORE_EXPORT QgsGradientStop
42{
43 public:
49 QgsGradientStop( double offset, const QColor &color );
50
52 double offset;
54 QColor color;
55
56 bool operator==( const QgsGradientStop &other ) const { return other.color == color && qgsDoubleNear( other.offset, offset ) && other.mColorSpec == mColorSpec && other.mDirection == mDirection; }
57
58 bool operator!=( const QgsGradientStop &other ) const { return !( *this == other ); }
59
69 QColor::Spec colorSpec() const { return mColorSpec; }
70
82 void setColorSpec( QColor::Spec spec );
83
94 Qgis::AngularDirection direction() const { return mDirection; }
95
107
108 private:
109 QColor::Spec mColorSpec = QColor::Spec::Rgb;
111 InterpolateColorFunc mFunc = nullptr;
112
114};
115
117typedef QList<QgsGradientStop> QgsGradientStopsList;
118
119// these are the QGIS branding colors, exaggerated a bit to make a default ramp with greater color variation
120// then the official QGIS color gradient!
121#define DEFAULT_GRADIENT_COLOR1 QColor( 69, 116, 40 )
122#define DEFAULT_GRADIENT_COLOR2 QColor( 188, 220, 60 )
123
130class CORE_EXPORT QgsGradientColorRamp : public QgsColorRamp
131{
132 public:
141 QgsGradientColorRamp( const QColor &color1 = DEFAULT_GRADIENT_COLOR1, const QColor &color2 = DEFAULT_GRADIENT_COLOR2, bool discrete = false, const QgsGradientStopsList &stops = QgsGradientStopsList() );
142
144 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
145
146 int count() const override { return mStops.count() + 2; }
147 double value( int index ) const override;
148 QColor color( double value ) const override;
149
155 static QString typeString() { return u"gradient"_s; }
156
157 QString type() const override;
158 void invert() override;
159 QgsGradientColorRamp *clone() const override SIP_FACTORY;
160 QVariantMap properties() const override;
161
167 QColor color1() const { return mColor1; }
168
174 QColor color2() const { return mColor2; }
175
182 void setColor1( const QColor &color ) { mColor1 = color; }
183
190 void setColor2( const QColor &color ) { mColor2 = color; }
191
197 bool isDiscrete() const { return mDiscrete; }
198
206 void setDiscrete( bool discrete ) { mDiscrete = discrete; }
207
215 void convertToDiscrete( bool discrete );
216
224 void setStops( const QgsGradientStopsList &stops );
225
231
236 QgsStringMap info() const { return mInfo; }
237
243 void setInfo( const QgsStringMap &info ) { mInfo = info; }
244
251 void addStopsToGradient( QGradient *gradient, double opacity = 1 ) const;
252
262 QColor::Spec colorSpec() const { return mColorSpec; }
263
275 void setColorSpec( QColor::Spec spec );
276
288
300
301 protected:
302 QColor mColor1;
303 QColor mColor2;
307 QColor::Spec mColorSpec = QColor::Spec::Rgb;
309
310 InterpolateColorFunc mFunc = nullptr;
311};
312
314
315#define DEFAULT_RANDOM_COUNT 10
316#define DEFAULT_RANDOM_HUE_MIN 0
317#define DEFAULT_RANDOM_HUE_MAX 359
318#define DEFAULT_RANDOM_VAL_MIN 200
319#define DEFAULT_RANDOM_VAL_MAX 240
320#define DEFAULT_RANDOM_SAT_MIN 100
321#define DEFAULT_RANDOM_SAT_MAX 240
322
328class CORE_EXPORT QgsLimitedRandomColorRamp : public QgsColorRamp
329{
330 public:
349 );
350
357 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
358
359 double value( int index ) const override;
360 QColor color( double value ) const override;
361
367 static QString typeString() { return u"random"_s; }
368
369 QString type() const override;
370 QgsLimitedRandomColorRamp *clone() const override SIP_FACTORY;
371 QVariantMap properties() const override;
372 int count() const override { return mCount; }
373
377 static QList<QColor> randomColors(
378 int count,
379 int hueMax = DEFAULT_RANDOM_HUE_MAX,
380 int hueMin = DEFAULT_RANDOM_HUE_MIN,
381 int satMax = DEFAULT_RANDOM_SAT_MAX,
382 int satMin = DEFAULT_RANDOM_SAT_MIN,
383 int valMax = DEFAULT_RANDOM_VAL_MAX,
384 int valMin = DEFAULT_RANDOM_VAL_MIN
385 );
386
391 void updateColors();
392
397 int hueMin() const { return mHueMin; }
398
403 int hueMax() const { return mHueMax; }
404
409 int satMin() const { return mSatMin; }
410
415 int satMax() const { return mSatMax; }
416
421 int valMin() const { return mValMin; }
422
427 int valMax() const { return mValMax; }
428
432 void setCount( int val ) { mCount = val; }
433
438 void setHueMin( int val ) { mHueMin = val; }
439
444 void setHueMax( int val ) { mHueMax = val; }
445
450 void setSatMin( int val ) { mSatMin = val; }
451
456 void setSatMax( int val ) { mSatMax = val; }
457
462 void setValMin( int val ) { mValMin = val; }
463
468 void setValMax( int val ) { mValMax = val; }
469
470 protected:
478 QList<QColor> mColors;
479};
480
489class CORE_EXPORT QgsRandomColorRamp : public QgsColorRamp
490{
491 public:
493
494 int count() const override;
495
496 double value( int index ) const override;
497
498 QColor color( double value ) const override;
499
506 virtual void setTotalColorCount( int colorCount );
507
513 static QString typeString() { return u"randomcolors"_s; }
514
515 QString type() const override;
516
517 QgsRandomColorRamp *clone() const override SIP_FACTORY;
518
519 QVariantMap properties() const override;
520
521 protected:
523 QList<QColor> mPrecalculatedColors;
524};
525
526
532class CORE_EXPORT QgsPresetSchemeColorRamp : public QgsColorRamp, public QgsColorScheme
533{
534 public:
539 QgsPresetSchemeColorRamp( const QList< QColor > &colors = QList< QColor >() );
540
546
553 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
554
560 bool setColors( const QgsNamedColorList &colors, const QString & = QString(), const QColor & = QColor() ) override
561 {
562 mColors = colors;
563 return true;
564 }
565
570 QList< QColor > colors() const;
571
572 double value( int index ) const override;
573 QColor color( double value ) const override;
574
580 static QString typeString() { return u"preset"_s; }
581
582 QString type() const override;
583 void invert() override;
584 QgsPresetSchemeColorRamp *clone() const override SIP_FACTORY;
585 QVariantMap properties() const override;
586 int count() const override;
587
588 QString schemeName() const override { return u"preset"_s; }
589 QgsNamedColorList fetchColors( const QString &context = QString(), const QColor &baseColor = QColor() ) override;
590 bool isEditable() const override { return true; }
591
592 private:
593 QgsNamedColorList mColors;
594};
595
596
597#define DEFAULT_COLORBREWER_SCHEMENAME "Spectral"
598#define DEFAULT_COLORBREWER_COLORS 5
599
605class CORE_EXPORT QgsColorBrewerColorRamp : public QgsColorRamp
606{
607 public:
615
622 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY;
623
624 double value( int index ) const override;
625 QColor color( double value ) const override;
626
632 static QString typeString() { return u"colorbrewer"_s; }
633
634 QString type() const override { return QgsColorBrewerColorRamp::typeString(); }
635 void invert() override;
636 QgsColorBrewerColorRamp *clone() const override SIP_FACTORY;
637 QVariantMap properties() const override;
638 int count() const override { return mColors; }
639
644 QString schemeName() const { return mSchemeName; }
645
650 int colors() const { return mColors; }
651
658 void setSchemeName( const QString &schemeName )
659 {
661 loadPalette();
662 }
663
670 void setColors( int colors )
671 {
672 mColors = colors;
673 loadPalette();
674 }
675
680 static QStringList listSchemeNames();
681
688 static QList<int> listSchemeVariants( const QString &schemeName );
689
690 protected:
692 void loadPalette();
693
694 QString mSchemeName;
696 QList<QColor> mPalette;
698};
699
700
701#define DEFAULT_CPTCITY_SCHEMENAME "cb/div/BrBG_" //change this
702#define DEFAULT_CPTCITY_VARIANTNAME "05"
703
711{
712 public:
720 QgsCptCityColorRamp( const QString &schemeName = DEFAULT_CPTCITY_SCHEMENAME, const QString &variantName = DEFAULT_CPTCITY_VARIANTNAME, bool inverted = false, bool doLoadFile = true );
721
730 QgsCptCityColorRamp( const QString &schemeName, const QStringList &variantList, const QString &variantName = QString(), bool inverted = false, bool doLoadFile = true );
731
733 static QgsColorRamp *create( const QVariantMap &properties = QVariantMap() ) SIP_FACTORY; // cppcheck-suppress duplInheritedMember
734
740 static QString typeString() { return u"cpt-city"_s; } // cppcheck-suppress duplInheritedMember
741
742 QString type() const override;
743
744 void invert() override;
745
746 QgsCptCityColorRamp *clone() const override SIP_FACTORY;
747 void copy( const QgsCptCityColorRamp *other );
748 QgsGradientColorRamp *cloneGradientRamp() const SIP_FACTORY;
749
750 QVariantMap properties() const override;
751
752 QString schemeName() const { return mSchemeName; }
753 QString variantName() const { return mVariantName; }
754 QStringList variantList() const { return mVariantList; }
755
756 // lazy loading - have to call loadPalette() explicitly
757 void setSchemeName( const QString &schemeName )
758 {
760 mFileLoaded = false;
761 }
762 void setVariantName( const QString &variantName )
763 {
765 mFileLoaded = false;
766 }
767 void setVariantList( const QStringList &variantList ) { mVariantList = variantList; }
768 void setName( const QString &schemeName, const QString &variantName = QString(), const QStringList &variantList = QStringList() )
769 {
773 mFileLoaded = false;
774 }
775
776 void loadPalette() { loadFile(); }
777 bool hasMultiStops() const { return mMultiStops; }
778
785 static QString fileNameForVariant( const QString &schema, const QString &variant ) SIP_SKIP;
786
787 QString fileName() const;
788 bool loadFile();
789 bool fileLoaded() const { return mFileLoaded; }
790
791 QString copyingFileName() const;
792 QString descFileName() const;
793 QgsStringMap copyingInfo() const;
794
795 protected:
796 QString mSchemeName;
798 QStringList mVariantList;
799 bool mFileLoaded = false;
800 bool mMultiStops = false;
802};
803
804// clazy:excludeall=qstring-allocations
805
806#endif
AngularDirection
Angular directions.
Definition qgis.h:3546
@ CounterClockwise
Counter-clockwise direction.
Definition qgis.h:3548
Color ramp utilising "Color Brewer" preset color schemes.
QColor color(double value) const override
Returns the color corresponding to a specified value.
int count() const override
Returns number of defined colors, or -1 if undefined.
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Returns a new QgsColorBrewerColorRamp color ramp created using the properties encoded in a string map...
QString type() const override
Returns a string representing the color ramp type.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QString schemeName() const
Returns the name of the color brewer color scheme.
int colors() const
Returns the number of colors in the ramp.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
QgsColorBrewerColorRamp(const QString &schemeName=DEFAULT_COLORBREWER_SCHEMENAME, int colors=DEFAULT_COLORBREWER_COLORS, bool inverted=false)
Constructor for QgsColorBrewerColorRamp.
void setSchemeName(const QString &schemeName)
Sets the name of the color brewer color scheme.
void setColors(int colors)
Sets the number of colors in the ramp.
void loadPalette()
Generates the scheme using the current name and number of colors.
Abstract base class for color ramps.
QgsColorScheme()=default
A color ramp from the CPT City collection.
void setName(const QString &schemeName, const QString &variantName=QString(), const QStringList &variantList=QStringList())
void setSchemeName(const QString &schemeName)
void setVariantList(const QStringList &variantList)
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
QgsCptCityColorRamp(const QString &schemeName=DEFAULT_CPTCITY_SCHEMENAME, const QString &variantName=DEFAULT_CPTCITY_VARIANTNAME, bool inverted=false, bool doLoadFile=true)
Constructor for QgsCptCityColorRamp.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Creates the symbol layer.
QStringList variantList() const
void setVariantName(const QString &variantName)
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
QString schemeName() const
QString variantName() const
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QgsGradientStopsList mStops
void setColor1(const QColor &color)
Sets the gradient start color.
void setColor2(const QColor &color)
Sets the gradient end color.
void setInfo(const QgsStringMap &info)
Sets additional info to attach to the gradient ramp (e.g., authorship notes).
bool isDiscrete() const
Returns true if the gradient is using discrete interpolation, rather than smoothly interpolating betw...
int count() const override
Returns number of defined colors, or -1 if undefined.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsColorRamp from a map of properties.
QgsStringMap info() const
Returns any additional info attached to the gradient ramp (e.g., authorship notes).
Qgis::AngularDirection mDirection
QColor::Spec colorSpec() const
Returns the color specification in which the color component interpolation will occur.
QColor color(double value) const override
Returns the color corresponding to a specified value.
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
QgsGradientColorRamp(const QColor &color1=DEFAULT_GRADIENT_COLOR1, const QColor &color2=DEFAULT_GRADIENT_COLOR2, bool discrete=false, const QgsGradientStopsList &stops=QgsGradientStopsList())
Constructor for QgsGradientColorRamp.
QColor color1() const
Returns the gradient start color.
void setDirection(Qgis::AngularDirection direction)
Sets the direction to traverse the color wheel using when interpolating hue-based color specification...
Qgis::AngularDirection direction() const
Returns the direction to traverse the color wheel using when interpolating hue-based color specificat...
QgsGradientStopsList stops() const
Returns the list of intermediate gradient stops for the ramp.
void setDiscrete(bool discrete)
Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating betwe...
InterpolateColorFunc mFunc
QColor color2() const
Returns the gradient end color.
Qgis::AngularDirection direction() const
Returns the direction to traverse the color wheel using when interpolating hue-based color specificat...
bool operator==(const QgsGradientStop &other) const
QColor::Spec colorSpec() const
Returns the color specification in which the color component interpolation will occur.
double offset
Relative positional offset, between 0 and 1.
bool operator!=(const QgsGradientStop &other) const
void setDirection(Qgis::AngularDirection direction)
Sets the direction to traverse the color wheel using when interpolating hue-based color specification...
friend class QgsGradientColorRamp
QColor color
Gradient color at stop.
QgsGradientStop(double offset, const QColor &color)
Constructor for QgsGradientStop.
Constrained random color ramp, which returns random colors based on preset parameters.
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
void setSatMin(int val)
Sets the minimum saturation for generated colors.
int count() const override
Returns number of defined colors, or -1 if undefined.
void setHueMin(int val)
Sets the minimum hue for generated colors.
void setSatMax(int val)
Sets the maximum saturation for generated colors.
QColor color(double value) const override
Returns the color corresponding to a specified value.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
int valMax() const
Returns the maximum value for generated colors.
int satMax() const
Returns the maximum saturation for generated colors.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Returns a new QgsLimitedRandomColorRamp color ramp created using the properties encoded in a string m...
void setHueMax(int val)
Sets the maximum hue for generated colors.
void setCount(int val)
Sets the number of colors contained in the ramp.
int hueMax() const
Returns the maximum hue for generated colors.
int hueMin() const
Returns the minimum hue for generated colors.
void setValMax(int val)
Sets the maximum value for generated colors.
int valMin() const
Returns the minimum value for generated colors.
QgsLimitedRandomColorRamp(int count=DEFAULT_RANDOM_COUNT, int hueMin=DEFAULT_RANDOM_HUE_MIN, int hueMax=DEFAULT_RANDOM_HUE_MAX, int satMin=DEFAULT_RANDOM_SAT_MIN, int satMax=DEFAULT_RANDOM_SAT_MAX, int valMin=DEFAULT_RANDOM_VAL_MIN, int valMax=DEFAULT_RANDOM_VAL_MAX)
Constructor for QgsLimitedRandomColorRamp.
void setValMin(int val)
Sets the minimum value for generated colors.
int satMin() const
Returns the minimum saturation for generated colors.
A scheme based color ramp consisting of a list of predefined colors.
bool setColors(const QgsNamedColorList &colors, const QString &=QString(), const QColor &=QColor()) override
Sets the list of colors used by the ramp.
QList< QColor > colors() const
Returns the list of colors used by the ramp.
QString schemeName() const override
Gets the name for the color scheme.
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
QVariantMap properties() const override
Returns a string map containing all the color ramp's properties.
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Returns a new QgsPresetSchemeColorRamp color ramp created using the properties encoded in a string ma...
QgsPresetSchemeColorRamp(const QList< QColor > &colors=QList< QColor >())
Constructor for QgsPresetSchemeColorRamp.
bool isEditable() const override
Returns whether the color scheme is editable.
A color ramp consisting of random colors, constrained within component ranges.
double value(int index) const override
Returns relative value between [0,1] of color at specified index.
QList< QColor > mPrecalculatedColors
QgsRandomColorRamp()=default
static QString typeString()
Returns the string identifier for QgsRandomColorRamp.
int count() const override
Returns number of defined colors, or -1 if undefined.
virtual void setTotalColorCount(int colorCount)
Sets the desired total number of unique colors for the resultant ramp.
QColor color(double value) const override
Returns the color corresponding to a specified value.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
QMap< QString, QString > QgsStringMap
Definition qgis.h:7475
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_FACTORY
Definition qgis_sip.h:83
#define DEFAULT_COLORBREWER_COLORS
#define DEFAULT_COLORBREWER_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MAX
#define DEFAULT_CPTCITY_SCHEMENAME
#define DEFAULT_RANDOM_HUE_MIN
#define DEFAULT_RANDOM_COUNT
#define DEFAULT_RANDOM_SAT_MAX
#define DEFAULT_RANDOM_SAT_MIN
#define DEFAULT_CPTCITY_VARIANTNAME
#define DEFAULT_GRADIENT_COLOR1
#define DEFAULT_RANDOM_VAL_MIN
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
#define DEFAULT_GRADIENT_COLOR2
#define DEFAULT_RANDOM_VAL_MAX
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)