Skip to main content

ColorMap

Struct ColorMap 

Source
pub struct ColorMap {
    pub name: String,
    pub stops: Vec<ColorStop>,
}
Expand description

A colormap with multiple color stops and smooth interpolation

Fields§

§name: String

Name of the colormap

§stops: Vec<ColorStop>

Ordered list of color stops

Implementations§

Source§

impl ColorMap

Source

pub fn new(name: impl Into<String>) -> Self

Create a new colormap with a given name

Examples found in repository?
examples/basic_usage.rs (line 21)
5fn main() {
6    // Load built-in colormap
7    println!("Loading Fire colormap...");
8    let fire = scala_chromatica::io::load_builtin_colormap("Fire")
9        .expect("Failed to load Fire colormap");
10
11    // Sample colors at different positions
12    println!("\nFire gradient samples:");
13    for i in 0..=10 {
14        let position = i as f64 / 10.0;
15        let color = fire.get_color(position);
16        println!("  {:.1}: RGB({}, {}, {})", position, color.r, color.g, color.b);
17    }
18
19    // Create custom gradient
20    println!("\nCreating custom gradient...");
21    let mut custom = ColorMap::new("RedBlue");
22    custom.add_stop(ColorStop::new(0.0, Color::new(255, 0, 0)));
23    custom.add_stop(ColorStop::new(0.5, Color::new(255, 255, 255)));
24    custom.add_stop(ColorStop::new(1.0, Color::new(0, 0, 255)));
25
26    println!("\nCustom gradient samples:");
27    for i in 0..=10 {
28        let position = i as f64 / 10.0;
29        let color = custom.get_color(position);
30        println!("  {:.1}: RGB({}, {}, {})", position, color.r, color.g, color.b);
31    }
32
33    // List all available colormaps
34    println!("\nListing all available colormaps...");
35    match scala_chromatica::io::list_available_colormaps() {
36        Ok(colormaps) => {
37            println!("Found {} colormap(s):", colormaps.len());
38            for info in colormaps {
39                let type_str = if info.is_builtin {
40                    "built-in"
41                } else {
42                    "custom"
43                };
44                println!("  - {} ({})", info.name, type_str);
45            }
46        }
47        Err(e) => eprintln!("Error listing colormaps: {}", e),
48    }
49
50    // Demonstrate HSV color creation
51    println!("\nHSV color examples:");
52    let red = Color::from_hsv(0.0, 1.0, 1.0);
53    println!("  Red (H=0):   RGB({}, {}, {})", red.r, red.g, red.b);
54
55    let green = Color::from_hsv(120.0, 1.0, 1.0);
56    println!("  Green (H=120): RGB({}, {}, {})", green.r, green.g, green.b);
57
58    let blue = Color::from_hsv(240.0, 1.0, 1.0);
59    println!("  Blue (H=240): RGB({}, {}, {})", blue.r, blue.g, blue.b);
60}
Source

pub fn with_stops(name: impl Into<String>, stops: Vec<ColorStop>) -> Self

Create a colormap with initial stops

Source

pub fn add_stop(&mut self, stop: ColorStop)

Add a color stop to the gradient

Examples found in repository?
examples/basic_usage.rs (line 22)
5fn main() {
6    // Load built-in colormap
7    println!("Loading Fire colormap...");
8    let fire = scala_chromatica::io::load_builtin_colormap("Fire")
9        .expect("Failed to load Fire colormap");
10
11    // Sample colors at different positions
12    println!("\nFire gradient samples:");
13    for i in 0..=10 {
14        let position = i as f64 / 10.0;
15        let color = fire.get_color(position);
16        println!("  {:.1}: RGB({}, {}, {})", position, color.r, color.g, color.b);
17    }
18
19    // Create custom gradient
20    println!("\nCreating custom gradient...");
21    let mut custom = ColorMap::new("RedBlue");
22    custom.add_stop(ColorStop::new(0.0, Color::new(255, 0, 0)));
23    custom.add_stop(ColorStop::new(0.5, Color::new(255, 255, 255)));
24    custom.add_stop(ColorStop::new(1.0, Color::new(0, 0, 255)));
25
26    println!("\nCustom gradient samples:");
27    for i in 0..=10 {
28        let position = i as f64 / 10.0;
29        let color = custom.get_color(position);
30        println!("  {:.1}: RGB({}, {}, {})", position, color.r, color.g, color.b);
31    }
32
33    // List all available colormaps
34    println!("\nListing all available colormaps...");
35    match scala_chromatica::io::list_available_colormaps() {
36        Ok(colormaps) => {
37            println!("Found {} colormap(s):", colormaps.len());
38            for info in colormaps {
39                let type_str = if info.is_builtin {
40                    "built-in"
41                } else {
42                    "custom"
43                };
44                println!("  - {} ({})", info.name, type_str);
45            }
46        }
47        Err(e) => eprintln!("Error listing colormaps: {}", e),
48    }
49
50    // Demonstrate HSV color creation
51    println!("\nHSV color examples:");
52    let red = Color::from_hsv(0.0, 1.0, 1.0);
53    println!("  Red (H=0):   RGB({}, {}, {})", red.r, red.g, red.b);
54
55    let green = Color::from_hsv(120.0, 1.0, 1.0);
56    println!("  Green (H=120): RGB({}, {}, {})", green.r, green.g, green.b);
57
58    let blue = Color::from_hsv(240.0, 1.0, 1.0);
59    println!("  Blue (H=240): RGB({}, {}, {})", blue.r, blue.g, blue.b);
60}
Source

pub fn remove_stop(&mut self, index: usize)

Remove a color stop by index (minimum 2 stops required)

Source

pub fn get_color(&self, position: f64) -> Color

Get color at a specific position (0.0 to 1.0) by interpolating between stops

Examples found in repository?
examples/basic_usage.rs (line 15)
5fn main() {
6    // Load built-in colormap
7    println!("Loading Fire colormap...");
8    let fire = scala_chromatica::io::load_builtin_colormap("Fire")
9        .expect("Failed to load Fire colormap");
10
11    // Sample colors at different positions
12    println!("\nFire gradient samples:");
13    for i in 0..=10 {
14        let position = i as f64 / 10.0;
15        let color = fire.get_color(position);
16        println!("  {:.1}: RGB({}, {}, {})", position, color.r, color.g, color.b);
17    }
18
19    // Create custom gradient
20    println!("\nCreating custom gradient...");
21    let mut custom = ColorMap::new("RedBlue");
22    custom.add_stop(ColorStop::new(0.0, Color::new(255, 0, 0)));
23    custom.add_stop(ColorStop::new(0.5, Color::new(255, 255, 255)));
24    custom.add_stop(ColorStop::new(1.0, Color::new(0, 0, 255)));
25
26    println!("\nCustom gradient samples:");
27    for i in 0..=10 {
28        let position = i as f64 / 10.0;
29        let color = custom.get_color(position);
30        println!("  {:.1}: RGB({}, {}, {})", position, color.r, color.g, color.b);
31    }
32
33    // List all available colormaps
34    println!("\nListing all available colormaps...");
35    match scala_chromatica::io::list_available_colormaps() {
36        Ok(colormaps) => {
37            println!("Found {} colormap(s):", colormaps.len());
38            for info in colormaps {
39                let type_str = if info.is_builtin {
40                    "built-in"
41                } else {
42                    "custom"
43                };
44                println!("  - {} ({})", info.name, type_str);
45            }
46        }
47        Err(e) => eprintln!("Error listing colormaps: {}", e),
48    }
49
50    // Demonstrate HSV color creation
51    println!("\nHSV color examples:");
52    let red = Color::from_hsv(0.0, 1.0, 1.0);
53    println!("  Red (H=0):   RGB({}, {}, {})", red.r, red.g, red.b);
54
55    let green = Color::from_hsv(120.0, 1.0, 1.0);
56    println!("  Green (H=120): RGB({}, {}, {})", green.r, green.g, green.b);
57
58    let blue = Color::from_hsv(240.0, 1.0, 1.0);
59    println!("  Blue (H=240): RGB({}, {}, {})", blue.r, blue.g, blue.b);
60}
Source

pub fn default_scheme() -> Self

Default HSV-based color scheme (smooth rainbow)

Source

pub fn fire_scheme() -> Self

Fire color scheme (black -> red -> orange -> yellow -> white)

Source

pub fn ocean_scheme() -> Self

Ocean color scheme (black -> deep blue -> cyan -> white)

Source

pub fn grayscale_scheme() -> Self

Grayscale color scheme (black -> gray -> white)

Source

pub fn rainbow_scheme() -> Self

Rainbow color scheme (full spectrum)

Trait Implementations§

Source§

impl Clone for ColorMap

Source§

fn clone(&self) -> ColorMap

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ColorMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ColorMap

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for ColorMap

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,