1
1
using System ;
2
- using System . Collections ;
3
2
using System . Collections . Generic ;
4
3
using System . Text ;
5
4
@@ -495,10 +494,13 @@ public string Name
495
494
/// <param name="author">author</param>
496
495
public Palette ( in string name = "Palette" , in string author = "Anonymous" )
497
496
{
498
- // TODO: Implement a Palette.Tag inner class which has a name
497
+ // TODO: Consider a Palette.Tag inner class which has a name
499
498
// and an int[] array which references indices in the palette.
500
499
// Each palete will have a list/array of tags, which allow the
501
500
// user to cluster together a palette into arbitrary groups.
501
+ // Problem is that it will be a challenge to keep tag indices
502
+ // up-to-date when the palette changes, entries are removed, etc.
503
+ // Maybe use a list of Entries, since they are classes.
502
504
this . Name = name ;
503
505
this . Author = author ;
504
506
}
@@ -694,6 +696,54 @@ public static Palette Concat (in Palette a, in Palette b, in Palette target)
694
696
return target ;
695
697
}
696
698
699
+ /// <summary>
700
+ /// Reverses the source palette.
701
+ /// </summary>
702
+ /// <param name="source">source palette</param>
703
+ /// <param name="target">target palette</param>
704
+ /// <returns>reversed palette</returns>
705
+ public static Palette Reverse ( in Palette source , in Palette target )
706
+ {
707
+ return Palette . Reverse ( source , 0 , source . entries . Length , target ) ;
708
+ }
709
+
710
+ /// <summary>
711
+ /// Reverses a subset of the source palette.
712
+ /// </summary>
713
+ /// <param name="source">source palette</param>
714
+ /// <param name="index">start index</param>
715
+ /// <param name="length">sample length</param>
716
+ /// <param name="target">target palette</param>
717
+ /// <returns>reversed palette</returns>
718
+ public static Palette Reverse ( in Palette source , in int index , in int length , in Palette target )
719
+ {
720
+ Entry [ ] sourceEntries = source . entries ;
721
+ int sourceLen = sourceEntries . Length ;
722
+
723
+ Entry [ ] reversedEntries = new Entry [ sourceLen ] ;
724
+ System . Array . Copy ( sourceEntries , 0 , reversedEntries , 0 , sourceLen ) ;
725
+
726
+ int valIdx = Utils . Mod ( index , sourceLen ) ;
727
+ int valLen = Utils . Clamp ( length , 1 , sourceLen ) ;
728
+ System . Array . Reverse ( reversedEntries , valIdx , valLen ) ;
729
+
730
+ if ( Object . ReferenceEquals ( source , target ) )
731
+ {
732
+ target . entries = reversedEntries ;
733
+ }
734
+ else
735
+ {
736
+ target . entries = Entry . Resize ( target . entries , sourceLen ) ;
737
+ for ( int i = 0 ; i < sourceLen ; ++ i )
738
+ {
739
+ Entry reversed = reversedEntries [ i ] ;
740
+ target . entries [ i ] . Set ( reversed . Color , reversed . Name ) ;
741
+ }
742
+ }
743
+
744
+ return target ;
745
+ }
746
+
697
747
/// <summary>
698
748
/// Returns a palette with three primary colors -- red, green and blue --
699
749
/// and three secondary colors -- yellow, cyan and magenta -- in the
@@ -721,20 +771,19 @@ public static Palette Rgb (in Palette target)
721
771
/// Returns a subset of a palette.
722
772
/// </summary>
723
773
/// <param name="source">source palette</param>
724
- /// <param name="startIndex ">start count </param>
725
- /// <param name="count ">sample count </param>
774
+ /// <param name="index ">start index </param>
775
+ /// <param name="length ">sample length </param>
726
776
/// <param name="target">target palette</param>
727
777
/// <returns>subset</returns>
728
- public static Palette Subset ( in Palette source , in int startIndex , in int count , in Palette target )
778
+ public static Palette Subset ( in Palette source , in int index , in int length , in Palette target )
729
779
{
730
- int valCount = Utils . Max ( 1 , count ) ;
731
-
732
780
Entry [ ] sourceEntries = source . entries ;
733
- Entry [ ] subsetEntries = new Entry [ valCount ] ;
734
- int srcLen = sourceEntries . Length ;
735
- for ( int i = 0 ; i < valCount ; ++ i )
781
+ int sourceLen = sourceEntries . Length ;
782
+ int valLen = Utils . Clamp ( length , 1 , sourceLen ) ;
783
+ Entry [ ] subsetEntries = new Entry [ valLen ] ;
784
+ for ( int i = 0 ; i < valLen ; ++ i )
736
785
{
737
- int k = Utils . Mod ( startIndex + i , srcLen ) ;
786
+ int k = Utils . Mod ( index + i , sourceLen ) ;
738
787
subsetEntries [ i ] = sourceEntries [ k ] ;
739
788
}
740
789
@@ -744,8 +793,8 @@ public static Palette Subset (in Palette source, in int startIndex, in int count
744
793
}
745
794
else
746
795
{
747
- target . entries = Entry . Resize ( target . entries , valCount ) ;
748
- for ( int i = 0 ; i < valCount ; ++ i )
796
+ target . entries = Entry . Resize ( target . entries , valLen ) ;
797
+ for ( int i = 0 ; i < valLen ; ++ i )
749
798
{
750
799
Entry subsetEntry = subsetEntries [ i ] ;
751
800
target . entries [ i ] . Set ( subsetEntry . Color , subsetEntry . Name ) ;
0 commit comments