14
14
import java .util .List ;
15
15
import java .util .function .Function ;
16
16
17
- public record CompoundGradient ( PositionedColor [] sorted ) implements Gradient {
17
+ public final class CompoundGradient implements Gradient {
18
18
public static final Codec <CompoundGradient > DIRECT_CODEC = RecordCodecBuilder .create (instance -> instance .group (
19
- PositionedColor .CODEC .listOf ().fieldOf ("colors" ).forGetter (CompoundGradient :: getColorList )
19
+ PositionedColor .CODEC .listOf ().fieldOf ("colors" ).forGetter (c -> c . sortedList )
20
20
).apply (instance , CompoundGradient ::new ));
21
21
22
22
public static CompoundGradient ofColors (List <Color > colors ) {
@@ -29,12 +29,20 @@ public static CompoundGradient ofColors(List<Color> colors) {
29
29
return new CompoundGradient (list );
30
30
}
31
31
32
- public static final Codec <CompoundGradient > CODEC = Codec .either (DIRECT_CODEC , Color .CODEC .listOf ()).xmap (e -> e .map (Function .identity (), CompoundGradient ::ofColors ), g -> g .isSimple () ? Either .right (g .rawColors ()) : Either .left (g ));
33
- public static final StreamCodec <ByteBuf , CompoundGradient > STREAM_CODEC = PositionedColor .STREAM_CODEC .apply (ByteBufCodecs .list ()).map (CompoundGradient ::new , CompoundGradient ::getColorList );
32
+ public static final Codec <CompoundGradient > CODEC = Codec .either (DIRECT_CODEC , Color .CODEC .listOf ()).xmap (e -> e .map (Function .identity (), CompoundGradient ::ofColors ), g -> g .isSimple () ? Either .right (g .getRawColors ()) : Either .left (g ));
33
+ public static final StreamCodec <ByteBuf , CompoundGradient > STREAM_CODEC = PositionedColor .STREAM_CODEC .apply (ByteBufCodecs .list ()).map (CompoundGradient ::new , c -> c .sortedList );
34
+
35
+ private final PositionedColor [] sorted ;
36
+ private final List <PositionedColor > sortedList ;
37
+
38
+ private CompoundGradient (PositionedColor [] sorted ) {
39
+ this .sorted = sorted ;
40
+ Arrays .sort (this .sorted );
41
+ this .sortedList = Arrays .asList (this .sorted );
42
+ }
34
43
35
44
public CompoundGradient (List <PositionedColor > colors ) {
36
- this (colors .toArray (new PositionedColor [0 ]));
37
- Arrays .sort (sorted );
45
+ this (colors .toArray (PositionedColor .EMPTY_ARRAY ));
38
46
}
39
47
40
48
@ Override
@@ -44,7 +52,7 @@ public Color get(float delta) {
44
52
} else if (delta <= 0F || sorted .length == 1 ) {
45
53
return sorted [0 ].color ();
46
54
} else if (delta >= 1F ) {
47
- return sorted [sorted .length - 1 ].color (). get ( 1F ) ;
55
+ return sorted [sorted .length - 1 ].color ();
48
56
}
49
57
50
58
var left = sorted [0 ];
@@ -60,7 +68,7 @@ public Color get(float delta) {
60
68
}
61
69
}
62
70
63
- return left .color (). lerp ( left . easing (). ease ( KMath .map (delta , left .position (), right .position (), 0F , 1F )) , right . color () );
71
+ return left .interpolate ( KMath .map (delta , left .position (), right .position (), 0F , 1F ), right );
64
72
}
65
73
66
74
@ Override
@@ -88,11 +96,11 @@ public boolean isSimple() {
88
96
return true ;
89
97
}
90
98
91
- public List <PositionedColor > getColorList () {
92
- return Arrays . asList ( sorted );
99
+ public List <PositionedColor > getColors () {
100
+ return List . copyOf ( sortedList );
93
101
}
94
102
95
- public List <Color > rawColors () {
103
+ public List <Color > getRawColors () {
96
104
var list = new ArrayList <Color >(sorted .length );
97
105
98
106
for (var c : sorted ) {
@@ -101,4 +109,20 @@ public List<Color> rawColors() {
101
109
102
110
return list ;
103
111
}
112
+
113
+ @ Override
114
+ public boolean equals (Object o ) {
115
+ return o == this || (o instanceof CompoundGradient c && sortedList .equals (c .sortedList ));
116
+ }
117
+
118
+ @ Override
119
+ public int hashCode () {
120
+ return sortedList .hashCode ();
121
+ }
122
+
123
+ @ Override
124
+ public String toString () {
125
+ return "CompoundGradient" + sortedList ;
126
+ }
127
+
104
128
}
0 commit comments