diff --git a/src/main/scala/li/cil/oc/util/PackedColor.scala b/src/main/scala/li/cil/oc/util/PackedColor.scala index dd31ed2fd..1d05b926f 100644 --- a/src/main/scala/li/cil/oc/util/PackedColor.scala +++ b/src/main/scala/li/cil/oc/util/PackedColor.scala @@ -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 diff --git a/src/main/scala/li/cil/oc/util/TextBuffer.scala b/src/main/scala/li/cil/oc/util/TextBuffer.scala index 0d77df197..3360f8533 100644 --- a/src/main/scala/li/cil/oc/util/TextBuffer.scala +++ b/src/main/scala/li/cil/oc/util/TextBuffer.scala @@ -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