I can’t find a library that implements some sort of RGB iterator that iterates from red (255-0-0) to yellow (255-255-0) to etc. Is it possible to implement it? Its struct should implement Iterator<Item=(f64,f64,f64)>
and perhaps, it contains max_iters
to change its length.
Searching for functions/methods in libraries, I have not found an implementation of it
1
I have made an implementation:
<code>pub struct RainbowIterator {
max: usize,
current: usize,
}
impl RainbowIterator {
pub fn new(max: usize) -> Self {
Self { max, current: 0 }
}
}
impl Iterator for RainbowIterator {
type Item = (u8, u8, u8);
fn next(&mut self) -> Option<Self::Item> {
let mut ret = (255, 255, 255);
if self.current >= self.max {
return None;
}
let progress: f64 = self.current as f64 / self.max as f64;
let subprogress = progress % (1.0 / 6.0);
let phase = (progress * 6.0) as u8;
if phase == 0 {
ret.2 *= 0;
ret.1 = (255.0 * progress) as u8;
} else if phase == 1 {
ret.2 *= 0;
ret.0 = 255 - (255.0 * progress) as u8;
} else if phase == 2 {
ret.0 *= 0;
ret.2 = (255.0 * progress) as u8;
} else if phase == 3 {
ret.0 *= 0;
ret.1 = 255 - (255.0 * progress) as u8;
} else if phase == 4 {
ret.1 *= 0;
ret.0 = (255.0 * progress) as u8;
} else {
ret.1 *= 0;
ret.2 = 255 - (255.0 * progress) as u8;
}
self.current += 1;
return Some(ret);
}
}
</code>
<code>pub struct RainbowIterator {
max: usize,
current: usize,
}
impl RainbowIterator {
pub fn new(max: usize) -> Self {
Self { max, current: 0 }
}
}
impl Iterator for RainbowIterator {
type Item = (u8, u8, u8);
fn next(&mut self) -> Option<Self::Item> {
let mut ret = (255, 255, 255);
if self.current >= self.max {
return None;
}
let progress: f64 = self.current as f64 / self.max as f64;
let subprogress = progress % (1.0 / 6.0);
let phase = (progress * 6.0) as u8;
if phase == 0 {
ret.2 *= 0;
ret.1 = (255.0 * progress) as u8;
} else if phase == 1 {
ret.2 *= 0;
ret.0 = 255 - (255.0 * progress) as u8;
} else if phase == 2 {
ret.0 *= 0;
ret.2 = (255.0 * progress) as u8;
} else if phase == 3 {
ret.0 *= 0;
ret.1 = 255 - (255.0 * progress) as u8;
} else if phase == 4 {
ret.1 *= 0;
ret.0 = (255.0 * progress) as u8;
} else {
ret.1 *= 0;
ret.2 = 255 - (255.0 * progress) as u8;
}
self.current += 1;
return Some(ret);
}
}
</code>
pub struct RainbowIterator {
max: usize,
current: usize,
}
impl RainbowIterator {
pub fn new(max: usize) -> Self {
Self { max, current: 0 }
}
}
impl Iterator for RainbowIterator {
type Item = (u8, u8, u8);
fn next(&mut self) -> Option<Self::Item> {
let mut ret = (255, 255, 255);
if self.current >= self.max {
return None;
}
let progress: f64 = self.current as f64 / self.max as f64;
let subprogress = progress % (1.0 / 6.0);
let phase = (progress * 6.0) as u8;
if phase == 0 {
ret.2 *= 0;
ret.1 = (255.0 * progress) as u8;
} else if phase == 1 {
ret.2 *= 0;
ret.0 = 255 - (255.0 * progress) as u8;
} else if phase == 2 {
ret.0 *= 0;
ret.2 = (255.0 * progress) as u8;
} else if phase == 3 {
ret.0 *= 0;
ret.1 = 255 - (255.0 * progress) as u8;
} else if phase == 4 {
ret.1 *= 0;
ret.0 = (255.0 * progress) as u8;
} else {
ret.1 *= 0;
ret.2 = 255 - (255.0 * progress) as u8;
}
self.current += 1;
return Some(ret);
}
}