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