lt-core/lt-stdlib/color.lt
2024-06-11 15:17:58 +02:00

63 lines
1.8 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export {
/* todo: allow machine.UInt8 in the VM
*/
let morph-rgb-to-hsv = λ{
{
red: _[0,1] ~ _256 ~ machine.UInt64;
green: _[0,1] ~ _256 ~ machine.UInt64;
blue: _[0,1] ~ _256 ~ machine.UInt64;
} : <Color sRGB>
~ RGB
~ <Vec3 _[0,1] ~ _256 ~ machine.UInt64>;
}
/*
-> <Color sRGB>
~ HSV
~ {
hue: Angle ~ Degrees ~ _0,360 ~ _360 ~ machine.UInt64 ;
sat: _[0,1] ~ _256 ~ machine.UInt64;
val: _[0,1] ~ _256 ~ machine.UInt64;
}
~ <Vec3 machine.UInt64>
*/
↦ {
let channel_max = int-max (int-max red green) blue;
let channel_min = int-min (int-min red green) blue;
let channel_delta = i- channel_max channel_min;
/* value */
channel_max;
/* saturation */
i/ (i* 255 channel_delta) channel_max;
/* hue */
i% (i/ (i* 60
if( int-eq channel_max red ) { i+ (i* 0 255) (i- green blue); }
else {
if( int-eq channel_max green ) { i+ (i* 2 255) (i- blue red); }
else { i+ (i* 4 255) (i- red green); };
}
)
channel_delta
)
360;
};
/*
let get-srgb-luminance =
-> _[0,1] ~ machine.Float64
λ{r:,g:,b:} : <Color sRGB> ~ RGB ~ <Vec3 _[0,1] ~ machine.Float64>
{
machine.Float64.mul red 0.22248840;
machine.Float64.mul green 0.71690369;
machine.Float64.mul blue 0.06060791;
machine.Float64.add;
machine.Float64.add;
// add (add (mul r 0.2) (mul g 0.7)) (mul b 0.6)
};
*/
}