Add Orb and continue working on rainbow
This commit is contained in:
parent
04147e0989
commit
48b3ea2e01
34
src/color.rs
34
src/color.rs
@ -8,6 +8,9 @@ impl Rgb {
|
|||||||
pub const fn to_tuple(self) -> (u8, u8, u8) {
|
pub const fn to_tuple(self) -> (u8, u8, u8) {
|
||||||
(self.0, self.1, self.2)
|
(self.0, self.1, self.2)
|
||||||
}
|
}
|
||||||
|
pub fn to_float_tuple(self) -> (f32, f32, f32) {
|
||||||
|
(f32::from(self.0), f32::from(self.1), f32::from(self.2))
|
||||||
|
}
|
||||||
// pub fn to_gamma_corrected_tuple(&self) -> (u8, u8, u8) {
|
// pub fn to_gamma_corrected_tuple(&self) -> (u8, u8, u8) {
|
||||||
// (
|
// (
|
||||||
// GAMMA_CORRECT[self.0 as usize],
|
// GAMMA_CORRECT[self.0 as usize],
|
||||||
@ -77,6 +80,37 @@ pub const RAINBOW: [Rgb; 7] = [
|
|||||||
Rgb(148, 0, 211), // V
|
Rgb(148, 0, 211), // V
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/// Merges a color by some factor
|
||||||
|
pub fn merge_colors(from_color: Rgb, to_color: Rgb, factor: f32) -> Rgb {
|
||||||
|
let (from_r, from_g, from_b) = from_color.to_float_tuple();
|
||||||
|
let (to_r, to_g, to_b) = to_color.to_float_tuple();
|
||||||
|
|
||||||
|
// TODO: Do not use as u8
|
||||||
|
let r = ((to_r - from_r) * factor + from_r) as u8;
|
||||||
|
let g = ((to_g - from_g) * factor + from_g) as u8;
|
||||||
|
let b = ((to_b - from_b) * factor + from_b) as u8;
|
||||||
|
|
||||||
|
Rgb(r, g, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Builds a color ramp of length `length` of the (exclusive) bounds of `from_color` to `to_color`
|
||||||
|
pub fn build_ramp(from_color: Rgb, to_color: Rgb, length: usize) -> Vec<Rgb> {
|
||||||
|
//let diff = i16::from(to_color) - i16::from(from_color);
|
||||||
|
let mut ret: Vec<Rgb> = vec![];
|
||||||
|
// for i in 1..=length {
|
||||||
|
// ()
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ret
|
||||||
|
// let (r_step,g_step,b_step) = (
|
||||||
|
// (to_color.r - from_color.r).div_euclid(length),
|
||||||
|
// (to_color.g - from_color.g).div_euclid(length),
|
||||||
|
// (to_color.b - from_color.b).div_euclid(length),
|
||||||
|
// );
|
||||||
|
// step = 255
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -4,11 +4,13 @@ pub mod collide;
|
|||||||
pub mod fade;
|
pub mod fade;
|
||||||
pub mod moving_pixel;
|
pub mod moving_pixel;
|
||||||
pub mod moving_rainbow;
|
pub mod moving_rainbow;
|
||||||
|
pub mod orb;
|
||||||
pub mod solid;
|
pub mod solid;
|
||||||
pub use collide::Collide;
|
pub use collide::Collide;
|
||||||
pub use fade::Fade;
|
pub use fade::Fade;
|
||||||
pub use moving_pixel::MovingPixel;
|
pub use moving_pixel::MovingPixel;
|
||||||
pub use moving_rainbow::MovingRainbow;
|
pub use moving_rainbow::MovingRainbow;
|
||||||
|
pub use orb::Orb;
|
||||||
pub use solid::Solid;
|
pub use solid::Solid;
|
||||||
|
|
||||||
pub trait Pattern: std::fmt::Debug {
|
pub trait Pattern: std::fmt::Debug {
|
||||||
|
@ -2,23 +2,31 @@ use super::Pattern;
|
|||||||
use crate::color::{Rgb, RAINBOW};
|
use crate::color::{Rgb, RAINBOW};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
//use std::collections::VecDeque;
|
||||||
|
//TODO Use a VecDeque
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct MovingRainbow {
|
pub struct MovingRainbow {
|
||||||
lights_buf: Vec<Rgb>,
|
lights_buf: Vec<Rgb>,
|
||||||
width: u8,
|
width: u8,
|
||||||
|
forward: bool,
|
||||||
}
|
}
|
||||||
impl MovingRainbow {
|
impl MovingRainbow {
|
||||||
pub const fn new(width: u8) -> Self {
|
pub const fn new(width: u8, forward: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
lights_buf: vec![],
|
lights_buf: vec![],
|
||||||
width,
|
width,
|
||||||
|
forward,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Pattern for MovingRainbow {
|
impl Pattern for MovingRainbow {
|
||||||
fn step(&mut self) -> Result<bool, ()> {
|
fn step(&mut self) -> Result<bool, ()> {
|
||||||
|
if self.forward {
|
||||||
|
self.lights_buf.rotate_left(1);
|
||||||
|
} else {
|
||||||
self.lights_buf.rotate_right(1);
|
self.lights_buf.rotate_right(1);
|
||||||
|
}
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
fn init(&mut self, num_lights: u16) -> Result<(), ()> {
|
fn init(&mut self, num_lights: u16) -> Result<(), ()> {
|
||||||
|
49
src/pattern/orb.rs
Normal file
49
src/pattern/orb.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use super::Pattern;
|
||||||
|
use crate::color::Rgb;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Orb {
|
||||||
|
lights_buf: Vec<Rgb>,
|
||||||
|
color: Rgb,
|
||||||
|
center_width: u8,
|
||||||
|
backoff_width: u8,
|
||||||
|
total_width: u8,
|
||||||
|
bounces: bool,
|
||||||
|
}
|
||||||
|
impl Orb {
|
||||||
|
pub const fn new(color: Rgb, center_width: u8, backoff_width: u8) -> Self {
|
||||||
|
Self {
|
||||||
|
lights_buf: vec![],
|
||||||
|
color,
|
||||||
|
center_width,
|
||||||
|
backoff_width,
|
||||||
|
total_width: center_width + backoff_width * 2,
|
||||||
|
bounces: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Pattern for Orb {
|
||||||
|
fn step(&mut self) -> Result<bool, ()> {
|
||||||
|
self.lights_buf.rotate_right(1);
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
fn init(&mut self, num_lights: u16) -> Result<(), ()> {
|
||||||
|
// if num_lights < 1 || num_lights > 255 {
|
||||||
|
// return Err(());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// self.lights_buf =
|
||||||
|
|
||||||
|
// self.lights_buf = RAINBOW
|
||||||
|
// .iter()
|
||||||
|
// .flat_map(|&x| iter::repeat(x).take(self.width.into()))
|
||||||
|
// .cycle()
|
||||||
|
// .take(buf_length.into())
|
||||||
|
// .collect();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn get_strip(&self) -> &[Rgb] {
|
||||||
|
&self.lights_buf
|
||||||
|
}
|
||||||
|
}
|
@ -65,10 +65,14 @@ fn parse_cmd(tx: &Sender<strip::Message>, s: &str) -> Result<(), String> {
|
|||||||
let color = parse_color(c, c, c)?;
|
let color = parse_color(c, c, c)?;
|
||||||
change_pattern(tx, Box::new(pattern::Solid::new(color)))
|
change_pattern(tx, Box::new(pattern::Solid::new(color)))
|
||||||
},
|
},
|
||||||
["r"] =>change_pattern(tx, Box::new(pattern::MovingRainbow::new(4))),
|
["r"] =>change_pattern(tx, Box::new(pattern::MovingRainbow::new(4, true))),
|
||||||
["r", w] => {
|
["r", w] => {
|
||||||
let width = w.parse::<u8>().map_err(|_| String::from("Width could not be parsed"))?;
|
let width = w.parse::<u8>().map_err(|_| String::from("Width could not be parsed"))?;
|
||||||
change_pattern(tx, Box::new(pattern::MovingRainbow::new(width)))
|
change_pattern(tx, Box::new(pattern::MovingRainbow::new(width, true)))
|
||||||
|
},
|
||||||
|
["r", w, f] => {
|
||||||
|
let width = w.parse::<u8>().map_err(|_| String::from("Width could not be parsed"))?;
|
||||||
|
change_pattern(tx, Box::new(pattern::MovingRainbow::new(width, ["t", "T"].contains(f))))
|
||||||
},
|
},
|
||||||
["b", r1, g1, b1, r2, g2, b2, r3, g3, b3] => {
|
["b", r1, g1, b1, r2, g2, b2, r3, g3, b3] => {
|
||||||
let left = parse_color(r1, g1, b1)?;
|
let left = parse_color(r1, g1, b1)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user