More palette index checks to avoid getting into a derpy state.

This commit is contained in:
Florian Nücke 2014-06-06 13:15:32 +02:00
parent bc202641e8
commit ac811de85d
2 changed files with 19 additions and 1 deletions

View File

@ -36,6 +36,12 @@ object PackedColor {
def inflate(value: Int): Int
def validate(value: Color) {
if (value.isPalette) {
throw new IllegalArgumentException("color palette not supported")
}
}
def deflate(value: Color): Byte
override def load(nbt: NBTTagCompound) {}
@ -48,12 +54,20 @@ object PackedColor {
override def inflate(value: Int) = if (value == 0) 0x000000 else 0xFFFFFF
override def deflate(value: Color) = (if (value.value == 0) 0 else 1).toByte
override def deflate(value: Color) = {
(if (value.value == 0) 0 else 1).toByte
}
}
abstract class PaletteFormat extends ColorFormat {
override def inflate(value: Int) = palette(math.max(0, math.min(palette.length - 1, value)))
override def validate(value: Color) {
if (value.isPalette && (value.value < 0 || value.value >= palette.length)) {
throw new IllegalArgumentException("invalid palette index")
}
}
override def deflate(value: Color) =
if (value.isPalette) (math.max(0, value.value) % palette.length).toByte
else palette.map(delta(value.value, _)).zipWithIndex.minBy(_._1)._2.toByte

View File

@ -26,15 +26,19 @@ class TextBuffer(var width: Int, var height: Int, initialFormat: PackedColor.Col
def foreground = _foreground
def foreground_=(value: PackedColor.Color) = {
format.validate(value)
_foreground = value
packed = PackedColor.pack(_foreground, _background, _format)
this
}
def background = _background
def background_=(value: PackedColor.Color) = {
format.validate(value)
_background = value
packed = PackedColor.pack(_foreground, _background, _format)
this
}
def format = _format