Add linear_gradient() helper

This commit is contained in:
Tangent 128 2019-03-17 01:04:03 -04:00
parent 491109885d
commit 51e5506cd8
2 changed files with 14 additions and 12 deletions

View File

@ -1,12 +1,13 @@
#![no_std]
#![no_main]
use core::iter::repeat;
use lights::{
gamma::GammaCorrector,
PixelIterator,
rgb::{
blend,
gray,
linear_gradient,
Rgb
}
};
@ -22,17 +23,14 @@ fn main() -> ! {
let teal = Rgb(0, 75, 150);
let purple = Rgb(75, 0, 150);
let mut gradient = (0..16u8).map(
|x| blend(red, yellow, gray(x * 16))
).chain((0..16u8).map(
|x| blend(yellow, green, gray(x * 16))
)).chain((0..16u8).map(
|x| blend(green, teal, gray(x * 16))
)).chain((0..16u8).map(
|x| blend(teal, purple, gray(x * 16))
)).chain((0..16u8).map(
|x| blend(purple, red, gray(x * 16))
)).cycle();
let segment_length = 11;
let mut gradient = linear_gradient(red, yellow, segment_length)
.chain(linear_gradient(yellow, green, segment_length))
.chain(linear_gradient(green, teal, segment_length))
.chain(linear_gradient(teal, purple, segment_length))
.chain(linear_gradient(purple, gray(0), segment_length))
.chain(repeat(gray(255)).take(60)).cycle();
loop {
gradient.clone().take(20).render_to(&mut lights);

View File

@ -60,3 +60,7 @@ pub const fn gray(gray: u8) -> Rgb {
pub fn blend(a: Rgb, b: Rgb, mix: Rgb) -> Rgb {
(a * Rgb(255 - mix.0, 255 - mix.1, 255 - mix.2)) + (b * mix)
}
pub fn linear_gradient(from: Rgb, to: Rgb, steps: usize) -> impl Iterator<Item = Rgb> + Clone {
(0..steps).map(move |x| blend(from, to, gray((x * 255 / steps) as u8)))
}