PageRenderTime 44ms CodeModel.GetById 32ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/newcode/src/com/prupe/mcpatcher/cc/Colorizer.java

https://bitbucket.org/Freso/mcpatcher
Java | 211 lines | 185 code | 26 blank | 0 comment | 34 complexity | 79ed3906da817eaf09e5ca615be9cc6b MD5 | raw file
  1package com.prupe.mcpatcher.cc;
  2
  3import com.prupe.mcpatcher.*;
  4import net.minecraft.src.Potion;
  5import net.minecraft.src.ResourceLocation;
  6
  7import java.util.Properties;
  8
  9public class Colorizer {
 10    private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.CUSTOM_COLORS);
 11
 12    static final ResourceLocation COLOR_PROPERTIES = TexturePackAPI.newMCPatcherResourceLocation("color.properties");
 13    private static Properties properties;
 14
 15    static final boolean useWaterColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "water", true);
 16    static final boolean useSwampColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "swamp", true);
 17    static final boolean useTreeColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "tree", true);
 18    static final boolean usePotionColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "potion", true);
 19    static final boolean useParticleColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "particle", true);
 20    static final boolean useFogColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "fog", true);
 21    static final boolean useCloudType = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "clouds", true);
 22    static final boolean useRedstoneColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "redstone", true);
 23    static final boolean useStemColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "stem", true);
 24    static final boolean useMapColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "map", true);
 25    static final boolean useDyeColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "dye", true);
 26    static final boolean useBlockColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "otherBlocks", true);
 27    static final boolean useTextColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "text", true);
 28    static final boolean useXPOrbColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "xporb", true);
 29    static final boolean useEggColors = Config.getBoolean(MCPatcherUtils.CUSTOM_COLORS, "egg", true);
 30
 31    public static final int COLOR_MAP_SWAMP_GRASS = 0;
 32    public static final int COLOR_MAP_SWAMP_FOLIAGE = 1;
 33    public static final int COLOR_MAP_PINE = 2;
 34    public static final int COLOR_MAP_BIRCH = 3;
 35    public static final int COLOR_MAP_FOLIAGE = 4;
 36    public static final int COLOR_MAP_WATER = 5;
 37    public static final int COLOR_MAP_UNDERWATER = 6;
 38    public static final int COLOR_MAP_FOG0 = 7;
 39    public static final int COLOR_MAP_SKY0 = 8;
 40    public static final int NUM_FIXED_COLOR_MAPS = 9;
 41
 42    static final ColorMap[] fixedColorMaps = new ColorMap[NUM_FIXED_COLOR_MAPS]; // bitmaps from FIXED_COLOR_MAPS
 43
 44    public static final float[] setColor = new float[3];
 45
 46    static {
 47        try {
 48            reset();
 49        } catch (Throwable e) {
 50            e.printStackTrace();
 51        }
 52        TexturePackChangeHandler.register(new TexturePackChangeHandler(MCPatcherUtils.CUSTOM_COLORS, 2) {
 53            @Override
 54            public void beforeChange() {
 55                reset();
 56            }
 57
 58            @Override
 59            public void afterChange() {
 60                reloadColorProperties();
 61                ColorizeBlock.reloadColorMaps(properties);
 62                if (useFogColors) {
 63                    ColorizeWorld.reloadFogColors(properties);
 64                }
 65                if (usePotionColors) {
 66                    ColorizeItem.reloadPotionColors(properties);
 67                }
 68                if (useSwampColors) {
 69                    ColorizeBlock.reloadSwampColors(properties);
 70                }
 71                if (useBlockColors) {
 72                    ColorizeBlock.reloadBlockColors(properties);
 73                }
 74                if (useParticleColors) {
 75                    ColorizeEntity.reloadParticleColors(properties);
 76                }
 77                if (useRedstoneColors) {
 78                    ColorizeBlock.reloadRedstoneColors(properties);
 79                }
 80                if (useStemColors) {
 81                    ColorizeBlock.reloadStemColors(properties);
 82                }
 83                if (useCloudType) {
 84                    ColorizeWorld.reloadCloudType(properties);
 85                }
 86                if (useMapColors) {
 87                    ColorizeItem.reloadMapColors(properties);
 88                }
 89                if (useDyeColors) {
 90                    ColorizeEntity.reloadDyeColors(properties);
 91                }
 92                if (useTextColors) {
 93                    ColorizeWorld.reloadTextColors(properties);
 94                }
 95                if (useXPOrbColors) {
 96                    ColorizeEntity.reloadXPOrbColors(properties);
 97                }
 98            }
 99        });
100    }
101
102    public static void setColorF(int color) {
103        intToFloat3(color, setColor);
104    }
105
106    private static void reset() {
107        properties = new Properties();
108
109        ColorizeBlock.reset();
110        Lightmap.reset();
111        ColorizeItem.reset();
112        ColorizeWorld.reset();
113        ColorizeEntity.reset();
114    }
115
116    private static void reloadColorProperties() {
117        if (TexturePackAPI.getProperties(COLOR_PROPERTIES, properties)) {
118            logger.finer("reloading %s", COLOR_PROPERTIES);
119        }
120    }
121
122    static String getStringKey(String[] keys, int index) {
123        if (keys != null && index >= 0 && index < keys.length && keys[index] != null) {
124            return keys[index];
125        } else {
126            return "" + index;
127        }
128    }
129
130    static void loadIntColor(String key, Potion potion) {
131        potion.color = loadIntColor(key, potion.color);
132    }
133
134    static boolean loadIntColor(String key, int[] color, int index) {
135        logger.config("%s=%06x", key, color[index]);
136        String value = properties.getProperty(key, "");
137        if (!value.equals("")) {
138            try {
139                color[index] = Integer.parseInt(value, 16);
140                return true;
141            } catch (NumberFormatException e) {
142            }
143        }
144        return false;
145    }
146
147    static int loadIntColor(String key, int color) {
148        logger.config("%s=%06x", key, color);
149        String value = properties.getProperty(key, "");
150        if (!value.equals("")) {
151            try {
152                return Integer.parseInt(value, 16);
153            } catch (NumberFormatException e) {
154            }
155        }
156        return color;
157    }
158
159    static void loadFloatColor(String key, float[] color) {
160        int intColor = float3ToInt(color);
161        intToFloat3(loadIntColor(key, intColor), color);
162    }
163
164    static void intToFloat3(int rgb, float[] f, int offset) {
165        if ((rgb & 0xffffff) == 0xffffff) {
166            f[offset] = f[offset + 1] = f[offset + 2] = 1.0f;
167        } else {
168            f[offset] = (float) (rgb & 0xff0000) / (float) 0xff0000;
169            f[offset + 1] = (float) (rgb & 0xff00) / (float) 0xff00;
170            f[offset + 2] = (float) (rgb & 0xff) / (float) 0xff;
171        }
172    }
173
174    static void intToFloat3(int rgb, float[] f) {
175        intToFloat3(rgb, f, 0);
176    }
177
178    static int float3ToInt(float[] f, int offset) {
179        return ((int) (255.0f * f[offset])) << 16 | ((int) (255.0f * f[offset + 1])) << 8 | (int) (255.0f * f[offset + 2]);
180    }
181
182    static int float3ToInt(float[] f) {
183        return float3ToInt(f, 0);
184    }
185
186    static float clamp(float f) {
187        if (f < 0.0f) {
188            return 0.0f;
189        } else if (f > 1.0f) {
190            return 1.0f;
191        } else {
192            return f;
193        }
194    }
195
196    static double clamp(double d) {
197        if (d < 0.0) {
198            return 0.0;
199        } else if (d > 1.0) {
200            return 1.0;
201        } else {
202            return d;
203        }
204    }
205
206    static void clamp(float[] f) {
207        for (int i = 0; i < f.length; i++) {
208            f[i] = clamp(f[i]);
209        }
210    }
211}