Skip to main content

Rgb

Rgb

import extras.scala.io.truecolor.Rgb

Rgb.fromInt(Int)

Rgb.fromInt(Int) - Valid

Rgb.fromInt(0x000000)
// res1: Either[String, Rgb] = Right(value = Rgb(0, 0, 0))
Rgb.fromInt(0x0000ff)
// res2: Either[String, Rgb] = Right(value = Rgb(0, 0, 255))
Rgb.fromInt(0x00ff00)
// res3: Either[String, Rgb] = Right(value = Rgb(0, 255, 0))
Rgb.fromInt(0xff0000)
// res4: Either[String, Rgb] = Right(value = Rgb(255, 0, 0))
Rgb.fromInt(0xffffff)
// res5: Either[String, Rgb] = Right(value = Rgb(255, 255, 255))

Rgb.fromInt(Int) - Invalid

Rgb.fromInt(0x000000 - 1)
// res6: Either[String, Rgb] = Left(value = "Invalid RGB color. Input: -1")
Rgb.fromInt(0xffffff + 1)
// res7: Either[String, Rgb] = Left(
// value = "Invalid RGB color. Input: 16777216"
// )

Rgb.unsafeFromInt(Int)

Rgb.unsafeFromInt(Int) - Valid

Rgb.unsafeFromInt(0x000000)
// res8: Rgb = Rgb(0, 0, 0)
Rgb.unsafeFromInt(0x0000ff)
// res9: Rgb = Rgb(0, 0, 255)
Rgb.unsafeFromInt(0x00ff00)
// res10: Rgb = Rgb(0, 255, 0)
Rgb.unsafeFromInt(0xff0000)
// res11: Rgb = Rgb(255, 0, 0)
Rgb.unsafeFromInt(0xffffff)
// res12: Rgb = Rgb(255, 255, 255)

Rgb.unsafeFromInt(Int) - Invalid

Rgb.unsafeFromInt(0x000000 - 1)
// java.lang.RuntimeException: Invalid RGB color. Input: -1
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromInt$1(Rgb.scala:46)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromInt(Rgb.scala:46)
// at repl.MdocSession$MdocApp0$$anonfun$1.apply(rgb.md:63)
// at repl.MdocSession$MdocApp0$$anonfun$1.apply(rgb.md:63)
Rgb.unsafeFromInt(0xffffff + 1)
// java.lang.RuntimeException: Invalid RGB color. Input: 16777216
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromInt$1(Rgb.scala:46)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromInt(Rgb.scala:46)
// at repl.MdocSession$MdocApp0$$anonfun$2.apply(rgb.md:73)
// at repl.MdocSession$MdocApp0$$anonfun$2.apply(rgb.md:73)
Rgb.fromRgbInts(0x00, 0x00, 0x00)
// res13: Either[String, Rgb] = Right(value = Rgb(0, 0, 0))
Rgb.fromRgbInts(0x00, 0x00, 0xff)
// res14: Either[String, Rgb] = Right(value = Rgb(0, 0, 255))
Rgb.fromRgbInts(0x00, 0xff, 0x00)
// res15: Either[String, Rgb] = Right(value = Rgb(0, 255, 0))
Rgb.fromRgbInts(0xff, 0x00, 0x00)
// res16: Either[String, Rgb] = Right(value = Rgb(255, 0, 0))
Rgb.fromRgbInts(0xff, 0xff, 0xff)
// res17: Either[String, Rgb] = Right(value = Rgb(255, 255, 255))
Rgb.unsafeFromRgbInts(0x00, 0x00, 0x00)
// res18: Rgb = Rgb(0, 0, 0)
Rgb.unsafeFromRgbInts(0x00, 0x00, 0xff)
// res19: Rgb = Rgb(0, 0, 255)
Rgb.unsafeFromRgbInts(0x00, 0xff, 0x00)
// res20: Rgb = Rgb(0, 255, 0)
Rgb.unsafeFromRgbInts(0xff, 0x00, 0x00)
// res21: Rgb = Rgb(255, 0, 0)
Rgb.unsafeFromRgbInts(0xff, 0xff, 0xff)
// res22: Rgb = Rgb(255, 255, 255)
Rgb.unsafeFromRgbInts(0x00, 0x00, 0x00 - 1)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Blue], Input: (Red: 0, Green: 0, Blue: -1)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$3.apply(rgb.md:119)
// at repl.MdocSession$MdocApp0$$anonfun$3.apply(rgb.md:119)
Rgb.unsafeFromRgbInts(0x00, 0x00, 0xff + 1)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Blue], Input: (Red: 0, Green: 0, Blue: 256)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$4.apply(rgb.md:129)
// at repl.MdocSession$MdocApp0$$anonfun$4.apply(rgb.md:129)
Rgb.unsafeFromRgbInts(0x00, 0x00 - 1, 0x00)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Green], Input: (Red: 0, Green: -1, Blue: 0)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$5.apply(rgb.md:139)
// at repl.MdocSession$MdocApp0$$anonfun$5.apply(rgb.md:139)
Rgb.unsafeFromRgbInts(0x00, 0xff + 1, 0x00)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Green], Input: (Red: 0, Green: 256, Blue: 0)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$6.apply(rgb.md:149)
// at repl.MdocSession$MdocApp0$$anonfun$6.apply(rgb.md:149)
Rgb.unsafeFromRgbInts(0x00 - 1, 0x00, 0x00)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Red], Input: (Red: -1, Green: 0, Blue: 0)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$7.apply(rgb.md:159)
// at repl.MdocSession$MdocApp0$$anonfun$7.apply(rgb.md:159)
Rgb.unsafeFromRgbInts(0xff + 1, 0x00, 0x00)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Red], Input: (Red: 256, Green: 0, Blue: 0)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$8.apply(rgb.md:169)
// at repl.MdocSession$MdocApp0$$anonfun$8.apply(rgb.md:169)
Rgb.unsafeFromRgbInts(0x00 - 1, 0x00 - 1, 0x00 - 1)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Red, Green, Blue], Input: (Red: -1, Green: -1, Blue: -1)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$9.apply(rgb.md:179)
// at repl.MdocSession$MdocApp0$$anonfun$9.apply(rgb.md:179)
Rgb.unsafeFromRgbInts(0x00, 0x00 - 1, 0xff + 1)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Green, Blue], Input: (Red: 0, Green: -1, Blue: 256)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$10.apply(rgb.md:189)
// at repl.MdocSession$MdocApp0$$anonfun$10.apply(rgb.md:189)
Rgb.unsafeFromRgbInts(0x00 - 1, 0xff + 1, 0x00)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Red, Green], Input: (Red: -1, Green: 256, Blue: 0)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$11.apply(rgb.md:199)
// at repl.MdocSession$MdocApp0$$anonfun$11.apply(rgb.md:199)
Rgb.unsafeFromRgbInts(0xff + 1, 0x00, 0x00 - 1)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Red, Blue], Input: (Red: 256, Green: 0, Blue: -1)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$12.apply(rgb.md:209)
// at repl.MdocSession$MdocApp0$$anonfun$12.apply(rgb.md:209)
Rgb.unsafeFromRgbInts(0xff + 1, 0xff + 1, 0xff + 1)
// java.lang.RuntimeException: Invalid RGB color. Invalid: [Red, Green, Blue], Input: (Red: 256, Green: 256, Blue: 256)
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromRgbInts$1(Rgb.scala:69)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromRgbInts(Rgb.scala:69)
// at repl.MdocSession$MdocApp0$$anonfun$13.apply(rgb.md:219)
// at repl.MdocSession$MdocApp0$$anonfun$13.apply(rgb.md:219)

Rgb.fromHexString(String)

Rgb.fromHexString(String) - Valid

Rgb.fromHexString("000000")
// res23: Either[String, Rgb] = Right(value = Rgb(0, 0, 0))
Rgb.fromHexString("#000000")
// res24: Either[String, Rgb] = Right(value = Rgb(0, 0, 0))
Rgb.fromHexString("0000ff")
// res25: Either[String, Rgb] = Right(value = Rgb(0, 0, 255))
Rgb.fromHexString("#0000ff")
// res26: Either[String, Rgb] = Right(value = Rgb(0, 0, 255))
Rgb.fromHexString("00ff00")
// res27: Either[String, Rgb] = Right(value = Rgb(0, 255, 0))
Rgb.fromHexString("#00ff00")
// res28: Either[String, Rgb] = Right(value = Rgb(0, 255, 0))
Rgb.fromHexString("ff0000")
// res29: Either[String, Rgb] = Right(value = Rgb(255, 0, 0))
Rgb.fromHexString("#ff0000")
// res30: Either[String, Rgb] = Right(value = Rgb(255, 0, 0))
Rgb.fromHexString("ffffff")
// res31: Either[String, Rgb] = Right(value = Rgb(255, 255, 255))
Rgb.fromHexString("#ffffff")
// res32: Either[String, Rgb] = Right(value = Rgb(255, 255, 255))

Rgb.fromHexString(String) - Invalid

Rgb.fromHexString("00000000")
// res33: Either[String, Rgb] = Left(value = "Invalid color hex: 00000000")
Rgb.fromHexString("#00000000")
// res34: Either[String, Rgb] = Left(value = "Invalid color hex: #00000000")
Rgb.fromHexString("ffffffff")
// res35: Either[String, Rgb] = Left(value = "Invalid color hex: ffffffff")
Rgb.fromHexString("#ffffffff")
// res36: Either[String, Rgb] = Left(value = "Invalid color hex: #ffffffff")

Rgb.unsafeFromHexString(String)

Rgb.unsafeFromHexString(String) - Valid

Rgb.unsafeFromHexString("000000")
// res37: Rgb = Rgb(0, 0, 0)
Rgb.unsafeFromHexString("#000000")
// res38: Rgb = Rgb(0, 0, 0)
Rgb.unsafeFromHexString("0000ff")
// res39: Rgb = Rgb(0, 0, 255)
Rgb.unsafeFromHexString("#0000ff")
// res40: Rgb = Rgb(0, 0, 255)
Rgb.unsafeFromHexString("00ff00")
// res41: Rgb = Rgb(0, 255, 0)
Rgb.unsafeFromHexString("#00ff00")
// res42: Rgb = Rgb(0, 255, 0)
Rgb.unsafeFromHexString("ff0000")
// res43: Rgb = Rgb(255, 0, 0)
Rgb.unsafeFromHexString("#ff0000")
// res44: Rgb = Rgb(255, 0, 0)
Rgb.unsafeFromHexString("ffffff")
// res45: Rgb = Rgb(255, 255, 255)
Rgb.unsafeFromHexString("#ffffff")
// res46: Rgb = Rgb(255, 255, 255)

Rgb.unsafeFromHexString(String) - Invalid

Rgb.unsafeFromHexString("00000000")
// java.lang.RuntimeException: Invalid color hex: 00000000
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromHexString$1(Rgb.scala:89)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromHexString(Rgb.scala:89)
// at repl.MdocSession$MdocApp0$$anonfun$14.apply(rgb.md:310)
// at repl.MdocSession$MdocApp0$$anonfun$14.apply(rgb.md:310)
Rgb.unsafeFromHexString("#00000000")
// java.lang.RuntimeException: Invalid color hex: #00000000
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromHexString$1(Rgb.scala:89)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromHexString(Rgb.scala:89)
// at repl.MdocSession$MdocApp0$$anonfun$15.apply(rgb.md:320)
// at repl.MdocSession$MdocApp0$$anonfun$15.apply(rgb.md:320)
Rgb.unsafeFromHexString("ffffffff")
// java.lang.RuntimeException: Invalid color hex: ffffffff
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromHexString$1(Rgb.scala:89)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromHexString(Rgb.scala:89)
// at repl.MdocSession$MdocApp0$$anonfun$16.apply(rgb.md:330)
// at repl.MdocSession$MdocApp0$$anonfun$16.apply(rgb.md:330)
Rgb.unsafeFromHexString("#ffffffff")
// java.lang.RuntimeException: Invalid color hex: #ffffffff
// at scala.sys.package$.error(package.scala:27)
// at extras.scala.io.truecolor.Rgb$.$anonfun$unsafeFromHexString$1(Rgb.scala:89)
// at scala.util.Either.fold(Either.scala:190)
// at extras.scala.io.truecolor.Rgb$.unsafeFromHexString(Rgb.scala:89)
// at repl.MdocSession$MdocApp0$$anonfun$17.apply(rgb.md:340)
// at repl.MdocSession$MdocApp0$$anonfun$17.apply(rgb.md:340)

To Colored String Form

Rgb.toAsciiEsc

Rgb.unsafeFromInt(0x000000).toAsciiEsc
// res47: String = "\u001b[38;2;0;0;0m"
Rgb.unsafeFromInt(0x0000ff).toAsciiEsc
// res48: String = "\u001b[38;2;0;0;255m"
Rgb.unsafeFromInt(0x00ff00).toAsciiEsc
// res49: String = "\u001b[38;2;0;255;0m"
Rgb.unsafeFromInt(0xff0000).toAsciiEsc
// res50: String = "\u001b[38;2;255;0;0m"
Rgb.unsafeFromInt(0xffffff).toAsciiEsc
// res51: String = "\u001b[38;2;255;255;255m"

Rgb.toHex

Rgb.unsafeFromInt(0x000000).toHex
// res52: String = "000000"
Rgb.unsafeFromInt(0x0000ff).toHex
// res53: String = "0000ff"
Rgb.unsafeFromInt(0x00ff00).toHex
// res54: String = "00ff00"
Rgb.unsafeFromInt(0xff0000).toHex
// res55: String = "ff0000"
Rgb.unsafeFromInt(0xffffff).toHex
// res56: String = "ffffff"

Rgb.toHexHtml

Rgb.unsafeFromInt(0x000000).toHexHtml
// res57: String = "#000000"
Rgb.unsafeFromInt(0x0000ff).toHexHtml
// res58: String = "#0000ff"
Rgb.unsafeFromInt(0x00ff00).toHexHtml
// res59: String = "#00ff00"
Rgb.unsafeFromInt(0xff0000).toHexHtml
// res60: String = "#ff0000"
Rgb.unsafeFromInt(0xffffff).toHexHtml
// res61: String = "#ffffff"

Rgb.toRgbInts

Rgb.unsafeFromInt(0x000000).toRgbInts
// res62: (Int, Int, Int) = (0, 0, 0)
Rgb.unsafeFromInt(0x0000ff).toRgbInts
// res63: (Int, Int, Int) = (0, 0, 255)
Rgb.unsafeFromInt(0x00ff00).toRgbInts
// res64: (Int, Int, Int) = (0, 255, 0)
Rgb.unsafeFromInt(0xff0000).toRgbInts
// res65: (Int, Int, Int) = (255, 0, 0)
Rgb.unsafeFromInt(0xffffff).toRgbInts
// res66: (Int, Int, Int) = (255, 255, 255)

Rgb.color(String)

println(s"${Rgb.unsafeFromInt(0x000000).color("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0x0000ff).color("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0x00ff00).color("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0xff0000).color("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0xffffff).color("Hello")} World")
// Hello World

Rgb.colored(String)

println(s"${Rgb.unsafeFromInt(0x000000).colored("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0x0000ff).colored("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0x00ff00).colored("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0xff0000).colored("Hello")} World")
// Hello World
println(s"${Rgb.unsafeFromInt(0xffffff).colored("Hello")} World")
// Hello World

syntax

To use Rgb syntax, import extras.scala.io.syntax.truecolor.rgb._.

import extras.scala.io.syntax.truecolor.rgb._

String.rgb(Int) and String.rgbed(Int)

"Hello".rgb(0x10e0ff)
// res78: String = "\u001b[38;2;16;224;255mHello"
"Hello".rgbed(0x10e0ff)
// res79: String = "\u001b[38;2;16;224;255mHello\u001b[0m"

Invalid RGB Value Handling

In .rgb(Int) and .rgbed(Int) syntax,

  • If the given RGB Int value is less than 0 (0x000000), it uses 0 (0x000000).
  • If the given RGB Int value is greater than 16777215 (0xffffff), it uses 16777215 (0xffffff).
"Hello".rgb(0x000000 - 1)
// res80: String = "\u001b[38;2;0;0;0mHello"
"Hello".rgb(0xffffff + 1)
// res81: String = "\u001b[38;2;255;255;255mHello"

"Hello".rgbed(0xffffff + 1)
// res82: String = "\u001b[38;2;255;255;255mHello\u001b[0m"
"Hello".rgbed(0xffffff + 1)
// res83: String = "\u001b[38;2;255;255;255mHello\u001b[0m"

String.rgb(Rgb) and String.rgbed(Rgb)

import extras.scala.io.truecolor.Rgb
Rgb.fromInt(0x11a02f)
.map(rgb => "Hello".rgb(rgb))
// res84: Either[String, String] = Right(value = "\u001b[38;2;17;160;47mHello")
Rgb.fromInt(0x11a02f)
.map(rgb => "Hello".rgbed(rgb))
// res85: Either[String, String] = Right(
// value = "\u001b[38;2;17;160;47mHello\u001b[0m"
// )

for {
a <- Rgb.fromInt(0x11a02f)
b <- Rgb.fromInt(0x00eab0)
} yield {
println("Hello".rgb(a))
println("Hello".rgb(b))
println("Hello".rgbed(a))
println("Hello".rgbed(b))
}
// Hello
// Hello
// Hello
// Hello
// res86: Either[String, Unit] = Right(value = ())