diff --git a/src/main/java/pl/wat/ms4ds/terrain/AltitudeColorMapper.java b/src/main/java/pl/wat/ms4ds/terrain/AltitudeColorMapper.java new file mode 100644 index 0000000..7000f6d --- /dev/null +++ b/src/main/java/pl/wat/ms4ds/terrain/AltitudeColorMapper.java @@ -0,0 +1,198 @@ +package pl.wat.ms4ds.terrain; + +import java.awt.Color; +import java.util.Arrays; + +public class AltitudeColorMapper { + + // Define the color stops and corresponding normalized values (0.0 to 1.0) + private static final float[] STOPS = {0.0f, 0.1f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}; + private static final Color[] COLORS2 = { + new Color(0, 0, 64), // Dark Navy Blue + new Color(0, 64, 204), // Medium Blue + new Color(51, 204, 51), // Bright Green + new Color(255, 255, 128),// Yellow + new Color(228, 96, 0), // Orange + new Color(96, 0, 0), // Red + new Color(158, 110, 110), + new Color(220, 220, 220), // Red + Color.WHITE // White + }; + private static final Color[] COLORS = { + new Color(40, 0, 40), + +// new Color(20, 0, 60), + + new Color(0, 0, 80), // DARK BLUE + +// new Color(0, 0, 160), +// new Color(0, 0, 200), + + new Color(0, 0, 255), // BLUE + +// new Color(0, 80, 255), +// new Color(0, 160, 255), +// new Color(0, 200, 255), + + new Color(0, 255, 255), + +// new Color(0, 255, 200), +// +// new Color(0, 255, 160), + + new Color(0, 255, 0), // GREEN + +// new Color(80, 255, 0), + +// new Color(80, 160, 20), + +// new Color(120, 255, 0), + +// new Color(200, 255, 0), + + new Color(255, 255, 0), // YELLOW + +// new Color(255, 200, 0), +// new Color(255, 120, 0), +// new Color(255, 60, 0), + + new Color(255, 0, 0), // RED + +// new Color(120, 0, 0), + + new Color(40, 0, 0), // DARK RED + +// new Color(80, 40, 40), +// new Color(120, 60, 60), +// new Color(160, 100, 100), +// new Color(200, 140, 140), + +// new Color(220, 180, 180), + new Color(255, 215, 215), + + Color.WHITE // White + }; + + /** + * RGB value representing the color in the default sRGB ColorModel. + * Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue. + * value = ((a & 0xFF) << 24) | + * ((r & 0xFF) << 16) | + * ((g & 0xFF) << 8) | + * ((b & 0xFF) << 0); + */ + private static final int[] COLORS_RGB = new int[7]; + +// static { +// Color c = new Color(0x37004E); +// int v = 0x37004E; +// int a = 0; +// int r = 0; +// int g = 0; +// int b = 50; +// int value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); +// COLORS_RGB[0] = value; +// a = 0; +// r = 0; +// g = 0; +// b = 0; +// value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); +// COLORS_RGB[0] = value; +// } + + private static final Color[] gamaKolorow; + + static { + Color[] gamaKolorowTemp = new Color[2000]; + int i = 0; + int r = 40; + int g = 0; + int b = 40; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + for (int j = 0; j < 40; j++) { + // r: 40->0, g:0, b: 40->80 + r--; + b++; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 175; j++) { + // r: 0, g:0, b: 80->255 + b++; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 255; j++) { + // r: 0, g:1->255, b: 255 + g++; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 255; j++) { + // r: 0, g:255, b: 254->0 + b--; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 255; j++) { + // r: 1->255, g:255, b: 0 + r++; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 255; j++) { + // r: 255, g:254->0, b: 0 + g--; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 215; j++) { + // r: 254->40, g: 0, b: 0 + r--; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + for (int j = 0; j < 215; j++) { + // r: 41->255, g: 0->215, b: 0->215 + r++; + g++; + b++; + gamaKolorowTemp[i++] = new Color(r, g, b, 255); + } + gamaKolorow = Arrays.copyOf(gamaKolorowTemp, i); + } + + /** + * Get the color corresponding to a given altitude value within a specified range. + * + * @param value The altitude value. + * @param minValue The minimum possible altitude. + * @param maxValue The maximum possible altitude. + * @return The interpolated color. + */ + public static Color getColorForAltitude(double value, double minValue, double maxValue) { + // Normalize the value to a 0.0 to 1.0 range + double normalizedValue = (value - minValue) / (maxValue - minValue); + + if (normalizedValue <= 0.0) return COLORS[0]; + if (normalizedValue >= 1.0) return COLORS[COLORS.length - 1]; + + // Find the correct interval in the color stops array + int stopIndex = 0; + while (stopIndex < STOPS.length - 1 && normalizedValue > STOPS[stopIndex + 1]) { + stopIndex++; + } + + // Interpolate between the two nearest colors + float start = STOPS[stopIndex]; + float end = STOPS[stopIndex + 1]; + float range = end - start; + float fraction = (float) ((normalizedValue - start) / range); + + Color c1 = COLORS[stopIndex]; + Color c2 = COLORS[stopIndex + 1]; + + int r = (int) (c1.getRed() + (c2.getRed() - c1.getRed()) * fraction); + int g = (int) (c1.getGreen() + (c2.getGreen() - c1.getGreen()) * fraction); + int b = (int) (c1.getBlue() + (c2.getBlue() - c1.getBlue()) * fraction); + + return new Color(r, g, b); + } + + static void main() { + Color c = gamaKolorow[0]; + } +}