golden-color/src/golden-color.cr

59 lines
1.4 KiB
Crystal

module GoldenColor
VERSION = "0.1.0"
struct Color
getter r : Float64
getter g : Float64
getter b : Float64
def initialize(@r = 0, @g = 0, @b = 0)
end
def self.from_rgb(r, g, b)
self.new r, g, b
end
def self.from_hsv(h, s, v)
h *= 6.0
chroma = s * v
x = chroma * (1.0 - (h.modulo(2.0) - 1.0).abs)
r, g, b = if h >= 0.0 && h <= 1.0
{chroma, x, 0.0}
elsif h >= 1.0 && h <= 2.0
{x, chroma, 0.0}
elsif h >= 2.0 && h <= 3.0
{0.0, chroma, x}
elsif h >= 3.0 && h <= 4.0
{0.0, x, chroma}
elsif h >= 4.0 && h <= 5.0
{x, 0.0, chroma}
else
{chroma, 0.0, x}
end
m = v - chroma
self.new(r + m, g + m, b + m)
end
def rgb
{ r, g, b }
end
end
class ColorGen
getter seed : Float64
def initialize(@saturation = 0.5,
@value = 0.95,
@seed = Random.rand(1.0))
end
def generate
color = Color.from_hsv @seed, @saturation, @value
@seed = (@seed + 0.618033988749895).modulo(1.0)
color
end
end
end