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