44import top .fpsmaster .features .settings .impl .utils .CustomColor ;
55
66import java .awt .*;
7+ import java .util .Arrays ;
78
89public class ColorSetting extends Setting <CustomColor > {
910
11+ public enum ColorType {
12+ STATIC ("colorsetting.type.static" ),
13+ WAVE ("colorsetting.type.breath" ),
14+ CHROMA ("colorsetting.type.chroma" ),
15+ RAINBOW ("colorsetting.type.rainbow" );
16+
17+ public final String i18nKey ;
18+
19+ ColorType (String i18nKey ) {
20+ this .i18nKey = i18nKey ;
21+ }
22+ }
23+
24+ private ColorType colorType = ColorType .STATIC ;
25+ private final ColorType [] availableTypes ;
26+ private Color latestResolvedColor ;
27+
1028 public ColorSetting (String name , CustomColor value , VisibleCondition visible ) {
1129 super (name , value , visible );
30+ this .availableTypes = new ColorType [] {ColorType .STATIC };
1231 }
1332
1433 public ColorSetting (String name , Color value , VisibleCondition visible ) {
1534 super (name , new CustomColor (value ), visible );
35+ this .availableTypes = new ColorType [] {ColorType .STATIC };
1636 }
1737
1838 public ColorSetting (String name , CustomColor value ) {
1939 super (name , value );
40+ this .availableTypes = new ColorType [] {ColorType .STATIC };
2041 }
2142
2243 public ColorSetting (String name , Color value ) {
2344 super (name , new CustomColor (value ));
45+ this .availableTypes = new ColorType [] {ColorType .STATIC };
46+ }
47+
48+ public ColorSetting (String name , CustomColor value , VisibleCondition visible , ColorType ... availableTypes ) {
49+ super (name , value , visible );
50+ this .availableTypes = normalizeTypes (availableTypes );
51+ }
52+
53+ public ColorSetting (String name , Color value , VisibleCondition visible , ColorType ... availableTypes ) {
54+ super (name , new CustomColor (value ), visible );
55+ this .availableTypes = normalizeTypes (availableTypes );
56+ }
57+
58+ public ColorSetting (String name , CustomColor value , ColorType ... availableTypes ) {
59+ super (name , value );
60+ this .availableTypes = normalizeTypes (availableTypes );
61+ }
62+
63+ public ColorSetting (String name , Color value , ColorType ... availableTypes ) {
64+ super (name , new CustomColor (value ));
65+ this .availableTypes = normalizeTypes (availableTypes );
2466 }
2567
2668 public int getRGB () {
27- return getValue ().getRGB ();
69+ return updateAndGetColor ().getRGB ();
70+ }
71+
72+ public int getRGB (float chromaOffset ) {
73+ return updateAndGetColor (chromaOffset ).getRGB ();
2874 }
2975
3076 public Color getColor () {
31- return getValue ().getColor ();
77+ return updateAndGetColor (0f );
78+ }
79+
80+ public Color getColor (float chromaOffset ) {
81+ return updateAndGetColor (chromaOffset );
82+ }
83+
84+ public Color updateAndGetColor () {
85+ return updateAndGetColor (0f );
86+ }
87+
88+ public Color updateAndGetColor (float chromaOffset ) {
89+ latestResolvedColor = resolveColor (getValue (), colorType , chromaOffset );
90+ return latestResolvedColor ;
91+ }
92+
93+ public int updateAndGetRGB () {
94+ return updateAndGetColor ().getRGB ();
95+ }
96+
97+ public int updateAndGetRGB (float chromaOffset ) {
98+ return updateAndGetColor (chromaOffset ).getRGB ();
99+ }
100+
101+ public Color getLatestResolvedColor () {
102+ return latestResolvedColor == null ? updateAndGetColor () : latestResolvedColor ;
103+ }
104+
105+ public static Color resolveColor (CustomColor value , ColorType type , float chromaOffset ) {
106+ long now = System .nanoTime ();
107+ float dynamicHue = (float ) ((now / 1_000_000_000.0 / 6.0 ) % 1.0 );
108+ float normalizedOffset = chromaOffset - (float ) Math .floor (chromaOffset );
109+
110+ switch (type ) {
111+ case WAVE :
112+ float alphaWave = value .alpha * (0.35f + 0.65f * (float ) ((Math .sin (now / 450_000_000.0 ) + 1.0 ) * 0.5 ));
113+ return new CustomColor (value .hue , value .brightness , value .saturation , alphaWave ).getColor ();
114+ case CHROMA :
115+ return new CustomColor ((value .hue + dynamicHue + normalizedOffset ) % 1.0f , value .brightness , value .saturation , value .alpha ).getColor ();
116+ case RAINBOW :
117+ return new CustomColor ((dynamicHue + normalizedOffset ) % 1.0f , value .brightness , value .saturation , value .alpha ).getColor ();
118+ case STATIC :
119+ default :
120+ return value .getColor ();
121+ }
122+ }
123+
124+ public ColorType [] getAvailableTypes () {
125+ return Arrays .copyOf (availableTypes , availableTypes .length );
126+ }
127+
128+ public ColorType getColorType () {
129+ return colorType ;
130+ }
131+
132+ public boolean supportsType (ColorType type ) {
133+ for (ColorType availableType : availableTypes ) {
134+ if (availableType == type ) {
135+ return true ;
136+ }
137+ }
138+ return false ;
139+ }
140+
141+ public void setColorType (ColorType type ) {
142+ if (type == null || !supportsType (type ) || colorType == type ) {
143+ return ;
144+ }
145+ CustomColor current = getValue ();
146+ CustomColor oldSnapshot = new CustomColor (current .hue , current .brightness , current .saturation , current .alpha );
147+ if (!fireValueChangeEvent (oldSnapshot , oldSnapshot )) {
148+ return ;
149+ }
150+ colorType = type ;
151+ notifyChangeListeners (oldSnapshot , oldSnapshot );
152+ }
153+
154+ public void cycleColorType () {
155+ int index = 0 ;
156+ for (int i = 0 ; i < availableTypes .length ; i ++) {
157+ if (availableTypes [i ] == colorType ) {
158+ index = i ;
159+ break ;
160+ }
161+ }
162+ setColorType (availableTypes [(index + 1 ) % availableTypes .length ]);
32163 }
33164
34165 public void setColor (float hue , float saturation , float brightness , float alpha ) {
@@ -52,6 +183,52 @@ public void setColor(Color color) {
52183 v .setColor (color );
53184 notifyChangeListeners (oldSnapshot , newSnapshot );
54185 }
186+
187+ private ColorType [] normalizeTypes (ColorType ... modes ) {
188+ if (modes == null || modes .length == 0 ) {
189+ return new ColorType [] {ColorType .STATIC };
190+ }
191+
192+ ColorType [] unique = new ColorType [modes .length ];
193+ int size = 0 ;
194+ for (ColorType mode : modes ) {
195+ if (mode == null ) {
196+ continue ;
197+ }
198+ boolean exists = false ;
199+ for (int i = 0 ; i < size ; i ++) {
200+ if (unique [i ] == mode ) {
201+ exists = true ;
202+ break ;
203+ }
204+ }
205+ if (!exists ) {
206+ unique [size ++] = mode ;
207+ }
208+ }
209+
210+ if (size == 0 ) {
211+ return new ColorType [] {ColorType .STATIC };
212+ }
213+
214+ ColorType [] result = Arrays .copyOf (unique , size );
215+ if (!supportsStatic (result )) {
216+ ColorType [] withStatic = new ColorType [size + 1 ];
217+ withStatic [0 ] = ColorType .STATIC ;
218+ System .arraycopy (result , 0 , withStatic , 1 , size );
219+ return withStatic ;
220+ }
221+ return result ;
222+ }
223+
224+ private boolean supportsStatic (ColorType [] types ) {
225+ for (ColorType type : types ) {
226+ if (type == ColorType .STATIC ) {
227+ return true ;
228+ }
229+ }
230+ return false ;
231+ }
55232}
56233
57234
0 commit comments