remove string constructor in RGBColor, use extension functions

This commit is contained in:
Bixilon 2021-05-05 15:04:40 +02:00
parent f58d9b6f8b
commit 82483282fe
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 55 additions and 44 deletions

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.entities.block
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
class BedBlockEntity(connection: PlayConnection) : BlockEntity(connection) {
@ -27,7 +28,7 @@ class BedBlockEntity(connection: PlayConnection) : BlockEntity(connection) {
color = nbt["color"]?.let {
when (it) {
is String -> {
RGBColor(it)
it.asColor()
}
is Number -> {
when (it.toInt()) {

View File

@ -18,6 +18,8 @@ import de.bixilon.minosoft.data.mappings.registry.RegistryItem
import de.bixilon.minosoft.data.mappings.registry.ResourceLocationDeserializer
import de.bixilon.minosoft.data.mappings.versions.VersionMapping
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.TintColorCalculator
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
@ -55,6 +57,7 @@ data class Biome(
}
companion object : ResourceLocationDeserializer<Biome> {
private val TODO_SWAMP_COLOR = "#6A7039".asColor()
override fun deserialize(mappings: VersionMapping?, resourceLocation: ResourceLocation, data: JsonObject): Biome {
check(mappings != null) { "VersionMapping is null!" }
return Biome(
@ -67,7 +70,7 @@ data class Biome(
waterFogColor = TintColorCalculator.getJsonColor(data["water_fog_color"]?.asInt ?: 0),
category = mappings.biomeCategoryRegistry.get(data["category"]?.asInt ?: -1) ?: DEFAULT_CATEGORY,
precipitation = mappings.biomePrecipitationRegistry.get(data["precipitation"]?.asInt ?: -1) ?: DEFAULT_PRECIPITATION,
skyColor = data["sky_color"]?.asInt?.let { RGBColor.noAlpha(it) } ?: RenderConstants.GRASS_FAILOVER_COLOR,
skyColor = data["sky_color"]?.asInt?.let { it.asRGBColor() } ?: RenderConstants.GRASS_FAILOVER_COLOR,
foliageColorOverride = TintColorCalculator.getJsonColor(data["foliage_color_override"]?.asInt ?: 0),
grassColorOverride = TintColorCalculator.getJsonColor(data["grass_color_override"]?.asInt ?: 0),
descriptionId = data["water_fog_color"]?.asString,
@ -89,7 +92,7 @@ data class Biome(
DARK_FOREST({ color: RGBColor -> color }), // ToDo: This rgb 2634762 should be added to this?
SWAMP({
// ToDo: Minecraft uses PerlinSimplexNoise here
RGBColor("#6A7039")
TODO_SWAMP_COLOR
}),
}
}

View File

@ -19,6 +19,7 @@ import de.bixilon.minosoft.data.mappings.registry.ResourceLocationDeserializer
import de.bixilon.minosoft.data.mappings.registry.Translatable
import de.bixilon.minosoft.data.mappings.versions.VersionMapping
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
import de.bixilon.minosoft.datafixer.EntityAttributeFixer.fix
data class StatusEffect(
@ -47,7 +48,7 @@ data class StatusEffect(
resourceLocation = resourceLocation,
category = StatusEffectCategories.NAME_MAP[data["category"].asString]!!,
translationKey = data["translation_key"]?.asString,
color = RGBColor.noAlpha(data["color"].asInt),
color = data["color"].asInt.asRGBColor(),
attributes.toMap(),
)
}

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.text
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import de.bixilon.minosoft.data.locale.minecraft.Translator
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.font.text.TextGetProperties
import de.bixilon.minosoft.gui.rendering.font.text.TextSetProperties
@ -94,7 +95,7 @@ class BaseComponent : ChatComponent {
val color = json["color"]?.asString?.let { colorName ->
if (colorName.startsWith("#")) {
RGBColor(colorName)
colorName.asColor()
} else {
ChatColors.getColorByName(colorName)
}

View File

@ -21,27 +21,25 @@ class RGBColor(val rgba: Int) : ChatCode {
constructor(red: Byte, green: Byte, blue: Byte, alpha: Byte = 0xFF.toByte()) : this(red.toInt() and 0xFF, green.toInt() and 0xFF, blue.toInt() and 0xFF, alpha.toInt() and 0xFF)
constructor(colorString: String) : this(colorString.toColorInt())
val alpha: @IntRange(from = 0.toLong(), to = 255.toLong()) Int
val alpha: @IntRange(from = 0L, to = 255L) Int
get() = rgba and 0xFF
val red: @IntRange(from = 0.toLong(), to = 255.toLong()) Int
val red: @IntRange(from = 0L, to = 255L) Int
get() = rgba ushr 24 and 0xFF
val floatRed: @IntRange(from = 0.toLong(), to = 1.toLong()) Float
val floatRed: @IntRange(from = 0L, to = 1L) Float
get() = red / COLOR_FLOAT_DIVIDER
val green: @IntRange(from = 0.toLong(), to = 255.toLong()) Int
val green: @IntRange(from = 0L, to = 255L) Int
get() = rgba ushr 16 and 0xFF
val floatGreen: @IntRange(from = 0.toLong(), to = 1.toLong()) Float
val floatGreen: @IntRange(from = 0L, to = 1L) Float
get() = green / COLOR_FLOAT_DIVIDER
val blue: @IntRange(from = 0.toLong(), to = 255.toLong()) Int
val blue: @IntRange(from = 0L, to = 255L) Int
get() = rgba ushr 8 and 0xFF
val floatBlue: @IntRange(from = 0.toLong(), to = 1.toLong()) Float
val floatBlue: @IntRange(from = 0L, to = 1L) Float
get() = blue / COLOR_FLOAT_DIVIDER
val rgb: Int
@ -63,31 +61,33 @@ class RGBColor(val rgba: Int) : ChatCode {
return if (alpha != 255) {
String.format("#%08X", rgba)
} else {
String.format("#%06X", 0xFFFFFF and rgba)
String.format("#%06X", rgb)
}
}
companion object {
private const val COLOR_FLOAT_DIVIDER = 255.0f
fun noAlpha(color: Int): RGBColor {
return RGBColor(color shl 8 or 0xFF)
}
fun String.toColor(): RGBColor {
return RGBColor(this)
}
fun String.toColorInt(): Int {
fun String.asColor(): RGBColor {
return RGBColor(let {
var colorString = this
if (colorString.startsWith("#")) {
colorString = colorString.substring(1)
}
return if (colorString.length == 6) {
return@let if (colorString.length == 6) {
Integer.parseUnsignedInt(colorString + "ff", 16)
} else {
Integer.parseUnsignedInt(colorString, 16)
}
})
}
fun Int.asRGBColor(): RGBColor {
return RGBColor(this shl 8 or 0xFF)
}
fun Int.asRGBAColor(): RGBColor {
return RGBColor(this)
}
fun mix(vararg colors: RGBColor): RGBColor {

View File

@ -15,25 +15,27 @@ package de.bixilon.minosoft.gui.rendering
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
import glm_.vec2.Vec2
object RenderConstants {
const val DISABLE_RENDERING = false
val DEFAULT_SKY_COLOR = RGBColor("#ecff89")
val WHITE_COLOR = RGBColor("#ffffff")
val BLACK_COLOR = RGBColor("#000000")
val DEFAULT_SKY_COLOR = "#ecff89".asColor()
val WHITE_COLOR = "#ffffff".asColor()
val BLACK_COLOR = "#000000".asColor()
val GRASS_FAILOVER_COLOR = RGBColor("#48B518")
val GRASS_FAILOVER_COLOR = "#48B518".asColor()
val GRASS_OUT_OF_BOUNDS_COLOR = RGBColor(-65281)
val GRASS_OUT_OF_BOUNDS_COLOR = (-65281).asRGBColor()
val LILY_PAD_INVENTORY_COLOR = RGBColor("#71C35C")
val LILY_PAD_BLOCK_COLOR = RGBColor("#208030")
val LILY_PAD_INVENTORY_COLOR = "#71C35C".asColor()
val LILY_PAD_BLOCK_COLOR = "#208030".asColor()
val EXPERIENCE_BAR_LEVEL_COLOR = RGBColor("#80ff20")
val HP_TEXT_COLOR = RGBColor("#ff1313")
val EXPERIENCE_BAR_LEVEL_COLOR = "#80ff20".asColor()
val HP_TEXT_COLOR = "#ff1313".asColor()
const val COLORMAP_SIZE = 255

View File

@ -17,6 +17,7 @@ import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.gui.input.camera.FrustumChangeCallback
import de.bixilon.minosoft.gui.input.key.RenderWindowInputHandler
import de.bixilon.minosoft.gui.modding.events.RenderingStateChangeEvent
@ -177,7 +178,7 @@ class RenderWindow(
// Make the window visible
GL.createCapabilities()
setSkyColor(RGBColor("#fffe7a"))
setSkyColor("#fffe7a".asColor())
Log.log(LogMessageType.RENDERING_LOADING) { "Enabling all open gl features (${stopwatch.labTime()})..." }
glEnable(GL_DEPTH_TEST)

View File

@ -20,6 +20,7 @@ import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.mappings.biomes.Biome
import de.bixilon.minosoft.data.mappings.blocks.BlockState
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
import de.bixilon.minosoft.data.world.World
import de.bixilon.minosoft.gui.rendering.textures.Texture
import glm_.vec3.Vec3i
@ -136,7 +137,7 @@ class TintColorCalculator(val world: World) {
if (color == 0) {
return null
}
return RGBColor.noAlpha(color)
return color.asRGBColor()
}
}
}

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.util.json
import com.squareup.moshi.*
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
object RGBColorSerializer : JsonAdapter<RGBColor>() {
@FromJson
@ -26,7 +27,7 @@ object RGBColorSerializer : JsonAdapter<RGBColor>() {
if (rgb == 0) {
return null
}
return RGBColor.noAlpha(rgb)
return rgb.asRGBColor()
}
@ToJson
@ -35,9 +36,9 @@ object RGBColorSerializer : JsonAdapter<RGBColor>() {
jsonWriter.nullValue()
return
}
if (color.rgba == 0) {
if (color.rgb == 0) {
jsonWriter.nullValue()
}
jsonWriter.value(color.rgba)
jsonWriter.value(color.rgb)
}
}