mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 10:34:06 +09:00
com.torvald → net.torvald
Former-commit-id: 375604da8a20a6ba7cd0a8d05a44add02b2d04f4 Former-commit-id: 287287c5920b07618174d7a7573f049d350ded66
This commit is contained in:
86
src/net/torvald/colourutil/Col216.kt
Normal file
86
src/net/torvald/colourutil/Col216.kt
Normal file
@@ -0,0 +1,86 @@
|
||||
package net.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
|
||||
}
|
||||
}
|
||||
72
src/net/torvald/colourutil/Col40
Normal file
72
src/net/torvald/colourutil/Col40
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.torvald.colourutil
|
||||
|
||||
import org.newdawn.slick.Color
|
||||
|
||||
/**
|
||||
* 40-Step RGB with builtin utils.
|
||||
* Created by minjaesong on 16-02-20.
|
||||
*/
|
||||
class Col40 : LimitedColours {
|
||||
|
||||
var raw: Char = ' '
|
||||
private set
|
||||
|
||||
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] shl 16 or (LOOKUP[g] shl 8) or LOOKUP[b])
|
||||
}
|
||||
|
||||
override fun create(raw: Int) {
|
||||
assertRaw(raw)
|
||||
this.raw = raw.toChar()
|
||||
}
|
||||
|
||||
override fun create(r: Int, g: Int, b: Int) {
|
||||
assertRGB(r, g, b)
|
||||
raw = (MUL_2 * r + MUL * g + b).toChar()
|
||||
}
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
constructor(c: Color) {
|
||||
create(
|
||||
Math.round(c.r * (MUL - 1)),
|
||||
Math.round(c.g * (MUL - 1)),
|
||||
Math.round(c.b * (MUL - 1)))
|
||||
}
|
||||
|
||||
private fun assertRaw(i: Int) {
|
||||
if (i >= COLOUR_DOMAIN_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()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Transient private val LOOKUP = intArrayOf(0, 7, 13, 20, 26, 33, 39, 46, 52, 59, 65, 72, 78, 85, 92, 98, 105, 111, 118, 124, 131, 137, 144, 150, 157, 163, 170, 177, 183, 190, 196, 203, 209, 216, 222, 229, 235, 242, 248, 255)
|
||||
|
||||
const val MUL = 40
|
||||
const val MUL_2 = MUL * MUL
|
||||
const val MAX_STEP = MUL - 1
|
||||
const val COLOUR_DOMAIN_SIZE = MUL_2 * MUL
|
||||
}
|
||||
}
|
||||
130
src/net/torvald/colourutil/Col4096.kt
Normal file
130
src/net/torvald/colourutil/Col4096.kt
Normal file
@@ -0,0 +1,130 @@
|
||||
package net.torvald.colourutil
|
||||
|
||||
import org.newdawn.slick.Color
|
||||
|
||||
/**
|
||||
* 12-bit (16-step) RGB with builtin utils.
|
||||
* Created by minjaesong on 16-01-23.
|
||||
*/
|
||||
class Col4096 : LimitedColours {
|
||||
|
||||
/**
|
||||
* Retrieve raw ARGB value
|
||||
* @return 0xARGB
|
||||
*/
|
||||
var raw: Short = 0
|
||||
private set
|
||||
|
||||
/**
|
||||
|
||||
* @param data
|
||||
*/
|
||||
constructor(data: Int) {
|
||||
create(data)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
* @param r 0-15
|
||||
* *
|
||||
* @param g 0-15
|
||||
* *
|
||||
* @param b 0-15
|
||||
*/
|
||||
constructor(r: Int, g: Int, b: Int) {
|
||||
create(r, g, b)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Col4096 colour and convert it to Slick Color
|
||||
* @param i
|
||||
* *
|
||||
* @return
|
||||
*/
|
||||
override fun createSlickColor(raw: Int): Color {
|
||||
assertRaw(raw)
|
||||
|
||||
val a: Int
|
||||
val r: Int
|
||||
val g: Int
|
||||
val b: Int
|
||||
|
||||
r = raw and 0xF00 shr 8
|
||||
g = raw and 0x0F0 shr 4
|
||||
b = raw and 0x00F
|
||||
|
||||
if (raw > 0xFFF) {
|
||||
a = raw and 0xF000 shr 12
|
||||
|
||||
return Color(
|
||||
r.shl(4) or r, g.shl(4) or g, b.shl(4) or b, a.shl(4) or a)
|
||||
}
|
||||
else {
|
||||
return Color(
|
||||
r.shl(4) or r, g.shl(4) or g, b.shl(4) or b)
|
||||
}
|
||||
}
|
||||
|
||||
override fun createSlickColor(r: Int, g: Int, b: Int): Color {
|
||||
assertARGB(0, r, g, b)
|
||||
return createSlickColor(r.shl(8) or g.shl(4) or b)
|
||||
}
|
||||
|
||||
fun createSlickColor(a: Int, r: Int, g: Int, b: Int): Color {
|
||||
assertARGB(a, r, g, b)
|
||||
return createSlickColor(a.shl(12) or r.shl(8) or g.shl(4) or b)
|
||||
}
|
||||
|
||||
override fun create(raw: Int) {
|
||||
assertRaw(raw)
|
||||
this.raw = (raw and 0xFFFF).toShort()
|
||||
}
|
||||
|
||||
override fun create(r: Int, g: Int, b: Int) {
|
||||
assertARGB(0, r, g, b)
|
||||
raw = (r.shl(8) or g.shl(4) or b).toShort()
|
||||
}
|
||||
|
||||
fun create(a: Int, r: Int, g: Int, b: Int) {
|
||||
assertARGB(a, r, g, b)
|
||||
raw = (a.shl(12) or r.shl(8) or g.shl(4) or b).toShort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to 3 byte values, for raster imaging.
|
||||
* @return byte[RR, GG, BB] e.g. 0x4B3 -> 0x44, 0xBB, 0x33
|
||||
*/
|
||||
fun toByteArray(): ByteArray {
|
||||
val ret = ByteArray(3)
|
||||
val r = (raw.toInt() and 0xF00) shr 8
|
||||
val g = (raw.toInt() and 0x0F0) shr 4
|
||||
val b = (raw.toInt() and 0x00F)
|
||||
|
||||
ret[0] = (r.shl(4) or r).toByte()
|
||||
ret[1] = (g.shl(4) or g).toByte()
|
||||
ret[2] = (b.shl(4) or b).toByte()
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
override fun toSlickColour(): Color = createSlickColor(raw.toUint())
|
||||
|
||||
private fun assertRaw(i: Int) {
|
||||
if (i > 0xFFFF || i < 0) {
|
||||
println("i: " + i.toString())
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
private fun assertARGB(a: Int, r: Int, g: Int, b: Int) {
|
||||
if (a !in 0..16 || r !in 0..16 || g !in 0..16 || b !in 0..16) {
|
||||
println("a: " + a.toString())
|
||||
println("r: " + r.toString())
|
||||
println("g: " + g.toString())
|
||||
println("b: " + b.toString())
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
fun Short.toUint(): Int = this.toInt() and 0xFFFF
|
||||
}
|
||||
15
src/net/torvald/colourutil/HSV.kt
Normal file
15
src/net/torvald/colourutil/HSV.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
package net.torvald.colourutil
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-03-10.
|
||||
*/
|
||||
/**
|
||||
* @param h : Hue 0-359
|
||||
* @param s : Saturation 0-1
|
||||
* @param v : Value (brightness in Adobe Photoshop(TM)) 0-1
|
||||
*/
|
||||
data class HSV(
|
||||
var h: Float,
|
||||
var s: Float,
|
||||
var v: Float
|
||||
)
|
||||
110
src/net/torvald/colourutil/HSVUtil.kt
Normal file
110
src/net/torvald/colourutil/HSVUtil.kt
Normal file
@@ -0,0 +1,110 @@
|
||||
package net.torvald.colourutil
|
||||
|
||||
import com.jme3.math.FastMath
|
||||
import org.newdawn.slick.Color
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-01-16.
|
||||
*/
|
||||
object HSVUtil {
|
||||
|
||||
/**
|
||||
* Convert HSV parameters to RGB color.
|
||||
* @param H 0-359 Hue
|
||||
* *
|
||||
* @param S 0-1 Saturation
|
||||
* *
|
||||
* @param V 0-1 Value
|
||||
* *
|
||||
* @return org.newdawn.slick.Color
|
||||
* *
|
||||
* @link http://www.rapidtables.com/convert/color/hsv-to-rgb.htm
|
||||
*/
|
||||
fun toRGB(H: Float, S: Float, V: Float): Color {
|
||||
var H = H
|
||||
H %= 360f
|
||||
|
||||
val C = V * S
|
||||
val X = C * (1 - FastMath.abs(
|
||||
H / 60f % 2 - 1))
|
||||
val m = V - C
|
||||
|
||||
var R_prime = java.lang.Float.NaN
|
||||
var G_prime = java.lang.Float.NaN
|
||||
var B_prime = java.lang.Float.NaN
|
||||
|
||||
if (H < 60) {
|
||||
R_prime = C
|
||||
G_prime = X
|
||||
B_prime = 0f
|
||||
}
|
||||
else if (H < 120) {
|
||||
R_prime = X
|
||||
G_prime = C
|
||||
B_prime = 0f
|
||||
}
|
||||
else if (H < 180) {
|
||||
R_prime = 0f
|
||||
G_prime = C
|
||||
B_prime = X
|
||||
}
|
||||
else if (H < 240) {
|
||||
R_prime = 0f
|
||||
G_prime = X
|
||||
B_prime = C
|
||||
}
|
||||
else if (H < 300) {
|
||||
R_prime = X
|
||||
G_prime = 0f
|
||||
B_prime = C
|
||||
}
|
||||
else if (H < 360) {
|
||||
R_prime = C
|
||||
G_prime = 0f
|
||||
B_prime = X
|
||||
}
|
||||
|
||||
return Color(
|
||||
R_prime + m, G_prime + m, B_prime + m)
|
||||
}
|
||||
|
||||
fun toRGB(hsv: HSV): Color {
|
||||
return toRGB(hsv.h, hsv.s, hsv.v)
|
||||
}
|
||||
|
||||
fun fromRGB(color: Color): HSV {
|
||||
val r = color.r
|
||||
val g = color.g
|
||||
val b = color.b
|
||||
|
||||
val rgbMin = FastMath.min(r, g, b)
|
||||
val rgbMax = FastMath.max(r, g, b)
|
||||
|
||||
var h: Float
|
||||
val s: Float
|
||||
val v = rgbMax
|
||||
|
||||
val delta = rgbMax - rgbMin
|
||||
|
||||
if (rgbMax != 0f)
|
||||
s = delta / rgbMax
|
||||
else {
|
||||
h = 0f
|
||||
s = 0f
|
||||
return HSV(h, s, v)
|
||||
}
|
||||
|
||||
if (r == rgbMax)
|
||||
h = (g - b) / delta
|
||||
else if (g == rgbMax)
|
||||
h = 2 + (b - r) / delta
|
||||
else
|
||||
h = 4 + (r - g) / delta
|
||||
|
||||
h *= 60f
|
||||
if (h < 0) h += 360f
|
||||
|
||||
return HSV(h, s, v)
|
||||
}
|
||||
|
||||
}
|
||||
17
src/net/torvald/colourutil/LimitedColours.kt
Normal file
17
src/net/torvald/colourutil/LimitedColours.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package net.torvald.colourutil
|
||||
|
||||
import org.newdawn.slick.Color
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-02-11.
|
||||
*/
|
||||
interface LimitedColours {
|
||||
|
||||
fun createSlickColor(raw: Int): Color
|
||||
fun createSlickColor(r: Int, g: Int, b: Int): Color
|
||||
|
||||
fun create(raw: Int)
|
||||
fun create(r: Int, g: Int, b: Int)
|
||||
|
||||
fun toSlickColour(): Color
|
||||
}
|
||||
Reference in New Issue
Block a user