Files
Terrarum/src/com/Torvald/ColourUtil/Col216.kt
Song Minjae a1e12e59d7 Conditional overscanning for lightmap, actor/uiContainers are now LinkedList, added methods to retrieve/delete actors
Former-commit-id: 866789452965a62871e87713fa6c450e16f99895
Former-commit-id: b7a9b20d2a648ca0f10d4cf2f0ffdf449c38c1ae
2016-04-10 15:38:58 +09:00

87 lines
1.9 KiB
Kotlin

package com.torvald.colourutil
import org.newdawn.slick.Color
/**
* 6-Step RGB with builtin utils.
* Created by minjaesong on 16-02-11.
*/
class Col216 : LimitedColours {
var raw: Byte = 0
private set
/**
* @param data
*/
constructor(data: Byte) {
create(data.toInt())
}
/**
* @param r 0-5
* *
* @param g 0-5
* *
* @param b 0-5
*/
constructor(r: Int, g: Int, b: Int) {
create(r, g, b)
}
override fun createSlickColor(raw: Int): Color {
assertRaw(raw)
val r = raw / MUL_2
val g = raw % MUL_2 / MUL
val b = raw % MUL
return createSlickColor(r, g, b)
}
override fun createSlickColor(r: Int, g: Int, b: Int): Color {
assertRGB(r, g, b)
return Color(LOOKUP[r], LOOKUP[g], LOOKUP[b])
}
override fun create(raw: Int) {
assertRaw(raw)
this.raw = raw.toByte()
}
override fun create(r: Int, g: Int, b: Int) {
assertRGB(r, g, b)
raw = (MUL_2 * r + MUL * g + b).toByte()
}
override fun toSlickColour(): Color = createSlickColor(raw.toUint())
private fun assertRaw(i: Int) {
if (i >= COLOUR_RANGE_SIZE || i < 0) {
println("i: " + i.toString())
throw IllegalArgumentException()
}
}
private fun assertRGB(r: Int, g: Int, b: Int) {
if (r !in 0..MAX_STEP || g !in 0..MAX_STEP || b !in 0..MAX_STEP) {
println("r: " + r.toString())
println("g: " + g.toString())
println("b: " + b.toString())
throw IllegalArgumentException()
}
}
fun Byte.toUint() = this.toInt() and 0xFF
companion object {
@Transient private val LOOKUP = intArrayOf(0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF)
const val MUL = 6
const val MUL_2 = MUL * MUL
const val MAX_STEP = MUL - 1
const val COLOUR_RANGE_SIZE = MUL_2 * MUL
}
}