Skip to content

Instantly share code, notes, and snippets.

@fusetim
Created August 13, 2020 14:33
Show Gist options
  • Save fusetim/024de16fef8689c70274ae9bdf6b8b50 to your computer and use it in GitHub Desktop.
Save fusetim/024de16fef8689c70274ae9bdf6b8b50 to your computer and use it in GitHub Desktop.
G13-Chroma
package fr.fusetim.g13-chroma;
import java.io.File
import java.io.PrintWriter
object Main extends App {
println("Hello world!")
val colors = (0, 1.0, 1.0) #:: ((120,1.0,1.0) #:: ((240,1.0,1.0) #:: chroma(0,0.9,1.0)));
val file = new File("/run/g13d/g13-0")
val writer = new PrintWriter(file)
for { rgb <- colors.map(x => {println(x); x}).map(hsv_to_rgb) }
{
val (r,g,b) = rgb
writer.write(s"rgb $r $g $b")
writer.flush()
println(s"rgb $r $g $b")
Thread.sleep(250)
}
writer.close()
def chroma(hsv: (Int,Double,Double)) : Stream[(Int, Double, Double)] = {
var (h,s,v) = hsv
h+=1;
if (h >= 360) {
h = 0;
}
return hsv #:: chroma((h,s,v))
}
def hsv_to_rgb(hsv: (Int, Double, Double)) : (Int, Int, Int) = {
var (h,s,v) = hsv
val c : Double = v*s
val h_ : Double= (h%360.0) / 60.0
val x : Double = c * (1.0 - (mod_+(h_, 2)-1.0).abs)
val m : Double = v - c
val (r1,g1,b1) = h_ match {
case z if z <= 1 => {(c,x,0.0)}
case z if z <= 2 => {(x,c,0.0)}
case z if z <= 3 => {(0.0,c,x)}
case z if z <= 4 => {(0.0,x,c)}
case z if z <= 5 => {(x,0.0,c)}
case z if z <= 6 => {(c,0.0,x)}
case _ => {(0.0,0.0,0.0)}
}
val r = Math.min(((r1+m)*256).intValue(), 255)
val g = Math.min(((g1+m)*256).intValue(), 255)
val b = Math.min(((b1+m)*256).intValue(), 255)
(r,g,b)
}
def mod_+(i: Double, n:Double) : Double = {
val m = i % n
if (m < 0) m+n else m
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment