Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.11 KB | None | 0 0
  1. package com.ramadoka.fhab
  2.  
  3. /**
  4.  * Created by ramadokayano on 18/12/14.
  5.  * RGBClass implementation
  6.  *
  7.  */
  8. class RGBColor(val alpha: Int, val red: Int, val green: Int, val blue: Int) {
  9.   // ---- attribute accessor -----
  10.  
  11.   def argb = Array(alpha, red, green, blue)
  12.  
  13.   def rgb = Array(red, green, blue)
  14.  
  15.   def this(a: Int, gray: Int) = this(a, gray, gray, gray)
  16.  
  17.   def this(a: Int, rgb: Array[Int]) = this(a, rgb(0), rgb(1), rgb(2))
  18.  
  19.   def this(pixel: Int) = this(
  20.     (pixel >> 24) & 0xFF,
  21.     (pixel >> 16) & 0xFF,
  22.     (pixel >> 8) & 0xFF,
  23.     pixel & 0xFF
  24.   )
  25.  
  26.  
  27.   /**
  28.    *
  29.    * @param threshold to determine whether a color would become black or zero
  30.    * @return either white or black
  31.    */
  32.   def toBW(threshold: RGBColor = STDColor.Gray): RGBColor = {
  33.     if (this > threshold) STDColor.White
  34.     else STDColor.Black
  35.   }
  36.  
  37.   /**
  38.    * grayscale value of the color, the value is expected to be bound between 0 - 255
  39.    * @return
  40.    */
  41.   def grayValue: Int = {
  42.     rgb.sum / 3
  43.   }
  44.  
  45.   /**
  46.    * A color which r, g, b is neither over 255 or lower than 0
  47.    * @return
  48.    */
  49.   def asValidColor(minimum: Int, maximum: Int): RGBColor = new RGBColor(
  50.     alpha,
  51.     Math.max(Math.min(red, maximum), minimum),
  52.     Math.max(Math.min(green, maximum), minimum),
  53.     Math.max(Math.min(blue, maximum), minimum)
  54.   )
  55.  
  56.   def asValidColor: RGBColor = asValidColor(0, 255)
  57.  
  58.   // ---- boolean operator ----
  59.   def defaultComparator: Int = Array(alpha, red, green, blue).sum
  60.  
  61.   def <(that: RGBColor): Boolean = {
  62.     val thatv = that.asValidColor
  63.     asValidColor.defaultComparator < thatv.defaultComparator
  64.   }
  65.  
  66.   def >(that: RGBColor): Boolean = {
  67.     val thatv = that.asValidColor
  68.     asValidColor.defaultComparator > thatv.defaultComparator
  69.   }
  70.  
  71.   def >=(that: RGBColor): Boolean = !(this < that)
  72.  
  73.   def <=(that: RGBColor): Boolean = !(this > that)
  74.  
  75.   def ==(that: RGBColor): Boolean = this.defaultComparator == that.defaultComparator
  76.  
  77.   // ---- arithmetic operator -----
  78.   def *(other: Int): RGBColor = new RGBColor(alpha, rgb.map((x: Int) => x * other))
  79.  
  80.   def *(other: Double): RGBColor = new RGBColor(alpha, rgb.map((x: Int) => (x * other).toInt))
  81.  
  82.   def /(other: Int): RGBColor = new RGBColor(alpha, rgb.map((x: Int) => x / other))
  83.  
  84.   def +(that: RGBColor): RGBColor = new RGBColor(
  85.     alpha,
  86.     (rgb, that.rgb).zipped.map((thi: Int, tha: Int) => thi + tha)
  87.   )
  88.  
  89.   def unary_- : RGBColor = this * -1
  90.  
  91.   def -(that: RGBColor): RGBColor = this + (-that)
  92.  
  93.   // ---- parser to another type -----
  94.   def toInt: Int = Array(
  95.     (this.alpha & 0xFF) << 24,
  96.     (this.red & 0xFF) << 16,
  97.     (this.green & 0xFF) << 8,
  98.     this.blue & 0xFF
  99.   ).sum
  100.  
  101.   override def toString = {
  102.     "\n(%s)".format(argb.mkString(", "))
  103.   }
  104. }
  105.  
  106.  
  107. object STDColor {
  108.   lazy val Red = new RGBColor(255, 255, 0, 0)
  109.   lazy val Green = new RGBColor(255, 0, 255, 0)
  110.   lazy val Blue = new RGBColor(255, 0, 0, 255)
  111.   lazy val White = new RGBColor(255, 255, 255, 255)
  112.   lazy val Gray = new RGBColor(255, 127, 127, 127)
  113.   lazy val Black = new RGBColor(255, 0, 0, 0)
  114.   lazy val Transparent = new RGBColor(0, 255, 255, 255)
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement