Starsector API
Loading...
Searching...
No Matches
ColorShifterUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.util;
2
3import java.awt.Color;
4import java.util.Iterator;
5import java.util.LinkedHashMap;
6import java.util.Map;
7
8public class ColorShifterUtil implements ColorShifterAPI {
9
10 public static class ShiftData2 {
11 public Color to;
12 public FaderUtil fader;
13 public float shift;
14 public boolean nudged;
15 }
16
17 protected Color base;
18 protected Color curr;
19 protected boolean useSquareOfProgress = true;
21
23 this.base = base;
24 this.curr = base;
25 }
26
27 public boolean isUseSquareOfProgress() {
29 }
30
32 this.useSquareOfProgress = useSquareOfProgress;
33 }
34
35 public Color getBase() {
36 return base;
37 }
38
39 public void setBase(Color base) {
40 this.base = base;
41 }
42
43 public Color getCurr() {
44 return curr;
45 }
46
47 public void shift(Object source, Color to, float durIn, float durOut, float shift) {
48 if (to == null) {
49 data.remove(source);
50 return;
51 }
52 ShiftData2 sd = data.get(source);
53 if (sd == null) {
54 sd = new ShiftData2();
55 sd.fader = new FaderUtil(0, durIn, durOut);
56 sd.fader.setBounceDown(true);
57 data.put(source, sd);
58 }
59 sd.to = to;
60 sd.shift = shift;
61 sd.fader.setDuration(durIn, durOut);
62 sd.fader.fadeIn();
63 sd.nudged = true;
64 }
65
66 public void advance(float amount) {
67 Iterator<ShiftData2> iter = data.values().iterator();
68 while (iter.hasNext()) {
69 ShiftData2 sd = iter.next();
70 if (!sd.nudged) sd.fader.fadeOut();
71 sd.nudged = false;
72 sd.fader.advance(amount);
73 if (sd.fader.isFadedOut()) {
74 iter.remove();
75 }
76 }
77 updateCurr();
78 }
79
80 public boolean isShifted() {
81 //return curr != base;
82 return !data.isEmpty();
83 }
84
85 protected void updateCurr() {
86 if (data.isEmpty()) {
87 curr = base;
88 return;
89 }
91 }
92
94 if (data == null || data.isEmpty()) {
95 return diffBase;
96 }
97
98 float totalWeight = 0f;
99 for (ShiftData2 sd : data.values()) {
100 float progress = sd.fader.getBrightness();
101 if (useSquareOfProgress) progress *= progress;
102 totalWeight += progress;
103 }
104// totalWeight = data.size();
105
106 if (totalWeight <= 0) {
107 return diffBase;
108 }
109
110 float red = (float)diffBase.getRed();
111 float green = (float)diffBase.getGreen();
112 float blue = (float)diffBase.getBlue();
113 float alpha = (float)diffBase.getAlpha();
114
115 for (ShiftData2 sd : data.values()) {
116 float progress = sd.fader.getBrightness();
117 if (useSquareOfProgress) progress *= progress;
118 float currWeight = totalWeight - progress + 1f;
119 red += ((float)sd.to.getRed() - (float)diffBase.getRed()) * sd.shift * progress / currWeight;
120 green += ((float)sd.to.getGreen() - (float)diffBase.getGreen()) * sd.shift * progress / currWeight;
121 blue += ((float)sd.to.getBlue() - (float)diffBase.getBlue()) * sd.shift * progress / currWeight;
122 alpha += ((float)sd.to.getAlpha() - (float)diffBase.getAlpha()) * sd.shift * progress / currWeight;
123 }
124
125 if (red > 255) red = 255;
126 if (green > 255) green = 255;
127 if (blue > 255) blue = 255;
128 if (alpha > 255) alpha = 255;
129 if (red < 0) red = 0;
130 if (green < 0) green = 0;
131 if (blue < 0) blue = 0;
132 if (alpha < 0) alpha = 0;
133
134 return new Color((int)red, (int)green, (int)blue, (int)alpha);
135 }
136
137 public static void main(String[] args) {
139
140 for (int i = 0; i < 10; i++) {
141 c.shift("c1", Color.green, 1f, 1f, 1f);
142 c.shift("c2", Color.blue, 1f, 1f, 0.5f);
143 c.advance(0.1f);
144 }
145
146 System.out.println(c.getCurr());
147
148// c.advance(0.1f);
149// System.out.println(c.getCurr());
150 }
151
152}
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
void shift(Object source, Color to, float durIn, float durOut, float shift)
void setUseSquareOfProgress(boolean useSquareOfProgress)