123 lines
4.1 KiB
Kotlin
123 lines
4.1 KiB
Kotlin
package com.icegps.geotools
|
|
|
|
import android.util.Log
|
|
|
|
/**
|
|
* @author tabidachinokaze
|
|
* @date 2025/11/25
|
|
*/
|
|
class SimplePalette(
|
|
private var range: ClosedFloatingPointRange<Double>
|
|
) {
|
|
fun setRange(range: ClosedFloatingPointRange<Double>) {
|
|
this.range = range
|
|
}
|
|
|
|
private val colors: Map<Int, String>
|
|
|
|
init {
|
|
colors = generateTerrainColorMap()
|
|
}
|
|
|
|
fun palette(value: Double?): String {
|
|
if (value == null) return "#00000000"
|
|
val minH = range.start
|
|
val maxH = range.endInclusive
|
|
val normalized = ((value - minH) / (maxH - minH)).coerceIn(0.0, 1.0)
|
|
return colors[(normalized * 255).toInt()] ?: "#00000000"
|
|
}
|
|
|
|
fun palette1(value: Double?): String {
|
|
return if (value == null) "#00000000" else {
|
|
// 假设您已经知道高度范围,或者动态计算
|
|
val minH = range.start
|
|
val maxH = range.endInclusive
|
|
val normalized = ((value - minH) / (maxH - minH)).coerceIn(0.0, 1.0)
|
|
val alpha = (normalized * 255).toInt()
|
|
String.format("#%02X%02X%02X", alpha, 0, 0)
|
|
}.also {
|
|
Log.d("simplePalette", "$value -> $it")
|
|
}
|
|
}
|
|
|
|
fun generateGrayscaleColorMap2(): MutableMap<Int, String> {
|
|
val colorMap = mutableMapOf<Int, String>()
|
|
|
|
// 定义关键灰度点
|
|
val black = Color(0, 0, 0) // 低地势 - 黑色
|
|
val darkGray = Color(64, 64, 64) // 过渡
|
|
val midGray = Color(128, 128, 128) // 中间
|
|
val lightGray = Color(192, 192, 192) // 过渡
|
|
val white = Color(255, 255, 255) // 高地势 - 白色
|
|
|
|
for (i in 0..255) {
|
|
val position = i / 255.0
|
|
|
|
val color = when {
|
|
position < 0.25 -> interpolateColor(black, darkGray, position / 0.25)
|
|
position < 0.5 -> interpolateColor(darkGray, midGray, (position - 0.25) / 0.25)
|
|
position < 0.75 -> interpolateColor(midGray, lightGray, (position - 0.5) / 0.25)
|
|
else -> interpolateColor(lightGray, white, (position - 0.75) / 0.25)
|
|
}
|
|
colorMap[i] = color.toHex()
|
|
}
|
|
|
|
return colorMap
|
|
}
|
|
|
|
fun generateGrayscaleColorMap(): MutableMap<Int, String> {
|
|
val colorMap = mutableMapOf<Int, String>()
|
|
|
|
for (i in 0..255) {
|
|
// 从黑色到白色的线性渐变
|
|
val grayValue = i
|
|
val color = Color(grayValue, grayValue, grayValue)
|
|
colorMap[i] = color.toHex()
|
|
}
|
|
|
|
return colorMap
|
|
}
|
|
|
|
fun generateTerrainColorMap(): MutableMap<Int, String> {
|
|
val colorMap = mutableMapOf<Int, String>()
|
|
|
|
// 定义关键颜色点
|
|
val blue = Color(0, 0, 255) // 低地势 - 蓝色
|
|
val cyan = Color(0, 255, 255) // 中间过渡
|
|
val green = Color(0, 255, 0) // 中间过渡
|
|
val yellow = Color(255, 255, 0) // 中间过渡
|
|
val red = Color(255, 0, 0) // 高地势 - 红色
|
|
|
|
for (i in 0..255) {
|
|
val position = i / 255.0
|
|
|
|
val color = when {
|
|
position < 0.25 -> interpolateColor(blue, cyan, position / 0.25)
|
|
position < 0.5 -> interpolateColor(cyan, green, (position - 0.25) / 0.25)
|
|
position < 0.75 -> interpolateColor(green, yellow, (position - 0.5) / 0.25)
|
|
else -> interpolateColor(yellow, red, (position - 0.75) / 0.25)
|
|
}
|
|
colorMap[i] = color.toHex()
|
|
}
|
|
|
|
return colorMap
|
|
}
|
|
|
|
fun interpolateColor(start: Color, end: Color, fraction: Double): Color {
|
|
val r = (start.red + (end.red - start.red) * fraction).toInt()
|
|
val g = (start.green + (end.green - start.green) * fraction).toInt()
|
|
val b = (start.blue + (end.blue - start.blue) * fraction).toInt()
|
|
return Color(r, g, b)
|
|
}
|
|
|
|
// Color类简化实现
|
|
class Color(val red: Int, val green: Int, val blue: Int) {
|
|
fun toArgb(): Int {
|
|
return (0xFF shl 24) or (red shl 16) or (green shl 8) or blue
|
|
}
|
|
|
|
fun toHex(): String {
|
|
return String.format("#%06X", toArgb() and 0xFFFFFF)
|
|
}
|
|
}
|
|
} |