@@ -738,3 +738,102 @@ bool QgsCptCityColorRamp::loadFile()
738
738
mFileLoaded = true ;
739
739
return true ;
740
740
}
741
+
742
+
743
+ //
744
+ // QgsPresetColorRamp
745
+ //
746
+
747
+ QgsPresetSchemeColorRamp::QgsPresetSchemeColorRamp ( const QList<QColor>& colors )
748
+ {
749
+ Q_FOREACH ( const QColor& color, colors )
750
+ {
751
+ mColors << qMakePair ( color, color.name () );
752
+ }
753
+ // need at least one color
754
+ if ( mColors .isEmpty () )
755
+ mColors << qMakePair ( QColor ( 250 , 75 , 60 ), QString ( " #fa4b3c" ) );
756
+ }
757
+
758
+ QgsPresetSchemeColorRamp::QgsPresetSchemeColorRamp ( const QgsNamedColorList& colors )
759
+ : mColors( colors )
760
+ {
761
+ // need at least one color
762
+ if ( mColors .isEmpty () )
763
+ mColors << qMakePair ( QColor ( 250 , 75 , 60 ), QString ( " #fa4b3c" ) );
764
+ }
765
+
766
+ QgsColorRamp* QgsPresetSchemeColorRamp::create ( const QgsStringMap& properties )
767
+ {
768
+ QgsNamedColorList colors;
769
+
770
+ int i = 0 ;
771
+ QString colorString = properties.value ( QString ( " preset_color_%1" ).arg ( i ), QString () );
772
+ QString colorName = properties.value ( QString ( " preset_color_name_%1" ).arg ( i ), QString () );
773
+ while ( !colorString.isEmpty () )
774
+ {
775
+ colors << qMakePair ( QgsSymbolLayerUtils::decodeColor ( colorString ), colorName );
776
+ i++;
777
+ colorString = properties.value ( QString ( " preset_color_%1" ).arg ( i ), QString () );
778
+ colorName = properties.value ( QString ( " preset_color_name_%1" ).arg ( i ), QString () );
779
+ }
780
+
781
+ return new QgsPresetSchemeColorRamp ( colors );
782
+ }
783
+
784
+ QList<QColor> QgsPresetSchemeColorRamp::colors () const
785
+ {
786
+ QList< QColor > l;
787
+ for ( int i = 0 ; i < mColors .count (); ++i )
788
+ {
789
+ l << mColors .at ( i ).first ;
790
+ }
791
+ return l;
792
+ }
793
+
794
+ double QgsPresetSchemeColorRamp::value ( int index ) const
795
+ {
796
+ if ( mColors .size () < 1 )
797
+ return 0 ;
798
+ return static_cast < double >( index ) / ( mColors .size () - 1 );
799
+ }
800
+
801
+ QColor QgsPresetSchemeColorRamp::color ( double value ) const
802
+ {
803
+ if ( value < 0 || value > 1 )
804
+ return QColor ();
805
+
806
+ int colorCnt = mColors .count ();
807
+ int colorIdx = qMin ( static_cast < int >( value * colorCnt ), colorCnt - 1 );
808
+
809
+ if ( colorIdx >= 0 && colorIdx < colorCnt )
810
+ return mColors .at ( colorIdx ).first ;
811
+
812
+ return QColor ();
813
+ }
814
+
815
+ QgsPresetSchemeColorRamp* QgsPresetSchemeColorRamp::clone () const
816
+ {
817
+ return new QgsPresetSchemeColorRamp ( *this );
818
+ }
819
+
820
+ QgsStringMap QgsPresetSchemeColorRamp::properties () const
821
+ {
822
+ QgsStringMap props;
823
+ for ( int i = 0 ; i < mColors .count (); ++i )
824
+ {
825
+ props.insert ( QString ( " preset_color_%1" ).arg ( i ), QgsSymbolLayerUtils::encodeColor ( mColors .at ( i ).first ) );
826
+ props.insert ( QString ( " preset_color_name_%1" ).arg ( i ), mColors .at ( i ).second );
827
+ }
828
+ return props;
829
+ }
830
+
831
+ int QgsPresetSchemeColorRamp::count () const
832
+ {
833
+ return mColors .count ();
834
+ }
835
+
836
+ QgsNamedColorList QgsPresetSchemeColorRamp::fetchColors ( const QString& , const QColor& )
837
+ {
838
+ return mColors ;
839
+ }
0 commit comments