palette — functions and constants
Palettes: the colours a thermal picture is drawn with.
Three things, and keeping them apart is most of understanding this module:
The built-ins. A closed set of seventeen tables compiled into
the library, named by Palette. Palette::all lists them,
Palette::colors reads one out, and
crate::Camera::set_palette applies one. A built-in survives a
configuration round trip as a number, which is why it is the cheaper
path and the one to persist.
The library. Every palette on the machine: the CSV files the
SDK installs plus any an operator dropped into a palette directory.
It is a list of names, not a second Palette; that enum is
a closed set and a file cannot join it. library::names
enumerates it, library::colors reads one, and
crate::Camera::set_palette_by_name applies one.
A table you built yourself. Any slice of Rgb24, handed to
crate::Camera::set_custom_palette. A file-backed palette is
applied through this same route, so there is no third way to
colorize a picture.
The gradient and the picture disagree about byte order
A palette entry is R-G-B. A colorized pixel in a frame's image is B-G-R-A. Blitting a colour table straight into a BGRA surface therefore draws the legend with red and blue swapped, which looks entirely plausible and is wrong.
Reading a gradient out
This one runs: it needs the library loaded but no camera.
use optris_sdk::{Palette, PALETTE_COLOR_COUNT};
let colors = Palette::IronBow.colors()?;
assert_eq!(PALETTE_COLOR_COUNT, colors.len());
let name = Palette::IronBow.display_name()?; // for a menu
assert!(!name.is_empty());
assert!(!Palette::IronBow.is_alarm()?);
# Ok::<(), optris_sdk::Error>(())
Listing what the machine has
use optris_sdk::palette::library;
for name in library::names()? {
let entry = library::info(&name)?;
// `builtin` is Some for one of the SDK's own, None for a file --
// a file-backed palette has no number to persist.
let _ = (entry.source, entry.builtin, library::file(&name)?);
}
assert!(!library::directories()?.is_empty());
# Ok::<(), optris_sdk::Error>(())
Supplying your own table
use optris_sdk::{Camera, Rgb24, SimulatedConfig, PALETTE_COLOR_COUNT};
let span = (PALETTE_COLOR_COUNT - 1) as u32;
let table: Vec<Rgb24> = (0..PALETTE_COLOR_COUNT)
.map(|i| {
let v = (i as u32 * 255 / span) as u8;
Rgb24 { r: v, g: 0, b: 255 - v }
})
.collect();
let mut camera = Camera::simulated(&SimulatedConfig::new())?;
camera.set_custom_palette("Furnace", &table)?;
assert_eq!(Some("Furnace".to_string()), camera.custom_palette_name()?);
camera.clear_custom_palette()?; // back to the built-in
assert_eq!(None, camera.custom_palette_name()?);
# Ok::<(), optris_sdk::Error>(())
pub const PALETTE_COLOR_COUNT: usize = sys::OPTRIS_SDK_PALETTE_COLOR_COUNT as usize;
How many entries a palette's colour table has.
Fixed, and the same for every palette, which makes it the required length of a custom table rather than merely a convenient buffer size: the SDK refuses any other length outright.