Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ramadoka.fhab
- /**
- * Created by ramadokayano on 18/12/14.
- * RGBClass implementation
- *
- */
- class RGBColor(val alpha: Int, val red: Int, val green: Int, val blue: Int) {
- // ---- attribute accessor -----
- def argb = Array(alpha, red, green, blue)
- def rgb = Array(red, green, blue)
- def this(a: Int, gray: Int) = this(a, gray, gray, gray)
- def this(a: Int, rgb: Array[Int]) = this(a, rgb(0), rgb(1), rgb(2))
- def this(pixel: Int) = this(
- (pixel >> 24) & 0xFF,
- (pixel >> 16) & 0xFF,
- (pixel >> 8) & 0xFF,
- pixel & 0xFF
- )
- /**
- *
- * @param threshold to determine whether a color would become black or zero
- * @return either white or black
- */
- def toBW(threshold: RGBColor = STDColor.Gray): RGBColor = {
- if (this > threshold) STDColor.White
- else STDColor.Black
- }
- /**
- * grayscale value of the color, the value is expected to be bound between 0 - 255
- * @return
- */
- def grayValue: Int = {
- rgb.sum / 3
- }
- /**
- * A color which r, g, b is neither over 255 or lower than 0
- * @return
- */
- def asValidColor(minimum: Int, maximum: Int): RGBColor = new RGBColor(
- alpha,
- Math.max(Math.min(red, maximum), minimum),
- Math.max(Math.min(green, maximum), minimum),
- Math.max(Math.min(blue, maximum), minimum)
- )
- def asValidColor: RGBColor = asValidColor(0, 255)
- // ---- boolean operator ----
- def defaultComparator: Int = Array(alpha, red, green, blue).sum
- def <(that: RGBColor): Boolean = {
- val thatv = that.asValidColor
- asValidColor.defaultComparator < thatv.defaultComparator
- }
- def >(that: RGBColor): Boolean = {
- val thatv = that.asValidColor
- asValidColor.defaultComparator > thatv.defaultComparator
- }
- def >=(that: RGBColor): Boolean = !(this < that)
- def <=(that: RGBColor): Boolean = !(this > that)
- def ==(that: RGBColor): Boolean = this.defaultComparator == that.defaultComparator
- // ---- arithmetic operator -----
- def *(other: Int): RGBColor = new RGBColor(alpha, rgb.map((x: Int) => x * other))
- def *(other: Double): RGBColor = new RGBColor(alpha, rgb.map((x: Int) => (x * other).toInt))
- def /(other: Int): RGBColor = new RGBColor(alpha, rgb.map((x: Int) => x / other))
- def +(that: RGBColor): RGBColor = new RGBColor(
- alpha,
- (rgb, that.rgb).zipped.map((thi: Int, tha: Int) => thi + tha)
- )
- def unary_- : RGBColor = this * -1
- def -(that: RGBColor): RGBColor = this + (-that)
- // ---- parser to another type -----
- def toInt: Int = Array(
- (this.alpha & 0xFF) << 24,
- (this.red & 0xFF) << 16,
- (this.green & 0xFF) << 8,
- this.blue & 0xFF
- ).sum
- override def toString = {
- "\n(%s)".format(argb.mkString(", "))
- }
- }
- object STDColor {
- lazy val Red = new RGBColor(255, 255, 0, 0)
- lazy val Green = new RGBColor(255, 0, 255, 0)
- lazy val Blue = new RGBColor(255, 0, 0, 255)
- lazy val White = new RGBColor(255, 255, 255, 255)
- lazy val Gray = new RGBColor(255, 127, 127, 127)
- lazy val Black = new RGBColor(255, 0, 0, 0)
- lazy val Transparent = new RGBColor(0, 255, 255, 255)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement