Verified Commit 6138180a authored by Soren's avatar Soren

Some helpers

parent 2088a7d9
package tf.bug.nose
case class Color[T: ColorFormat](t: T)
import tf.bug.nose
import tf.bug.nose.implicits._
trait ColorFormat[T] {
case class Color[C](c: C)(implicit colorFormat: ColorFormat[C]) {
def asRGB(t: T): RGBFormat
def asHSV(t: T): HSVFormat
def distance(t1: T, t2: T): Double
def average(ts: Traversable[T]): T
def rgb: RGBFormat = colorFormat.asRGB(c)
def hsv: HSVFormat = colorFormat.asHSV(c)
}
case class RGBFormat(red: Double, green: Double, blue: Double) {
object Color {
if (!Set(red, green, blue).forall(inRange(0.0, _, 1.0))) {
throw new IllegalArgumentException("All arguments must be within 0.0 and 1.0!")
def rgb(red: Double, green: Double, blue: Double): Color[RGBFormat] = {
Color(nose.RGBFormat(red, green, blue))
}
}
case class HSVFormat(hue: Int, saturation: Double, value: Double) {
if (!inRange(0, hue, 359)) {
throw new IllegalArgumentException("Hue must be within 0 and 359!")
def rgb(red: Int, green: Int, blue: Int): Color[RGBFormat] = {
Color(nose.RGBFormat(red / 255.0, green / 255.0, blue / 255.0))
}
if (!Set(saturation, value).forall(inRange(0.0, _, 1.0))) {
throw new IllegalArgumentException("Saturation and Value must be within 0.0 and 1.0!")
def rgb(rgb: Int): Color[RGBFormat] = {
val red = (rgb >> 16) & 0xFF
val green = (rgb >> 8) & 0xFF
val blue = rgb & 0xFF
Color.rgb(red, green, blue)
}
}
package tf.bug.nose
trait ColorFormat[T] {
def asRGB(t: T): RGBFormat
def asHSV(t: T): HSVFormat
def distance(t1: T, t2: T): Double
def average(ts: Traversable[T]): T
}
case class RGBFormat(red: Double, green: Double, blue: Double) {
if (!Set(red, green, blue).forall(inRange(0.0, _, 1.0))) {
throw new IllegalArgumentException("All arguments must be within 0.0 and 1.0!")
}
}
case class HSVFormat(hue: Int, saturation: Double, value: Double) {
if (!inRange(0, hue, 359)) {
throw new IllegalArgumentException("Hue must be within 0 and 359!")
}
if (!Set(saturation, value).forall(inRange(0.0, _, 1.0))) {
throw new IllegalArgumentException("Saturation and Value must be within 0.0 and 1.0!")
}
}
package tf.bug.nose
import java.awt
package object implicits {
implicit def rgbColorFormat: ColorFormat[RGBFormat] = new ColorFormat[RGBFormat] {
......@@ -77,4 +79,10 @@ package object implicits {
}
}
implicit def asAWT[C](c: Color[C]): awt.Color = {
val r = c.rgb
new awt.Color((r.red * 255).toInt, (r.green * 255).toInt, (r.blue * 255).toInt)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment