The Palette Library
Where the SDK looks for palette files on this machine, how to list and use one, and what happens when a file is wrong. The NuGet case is the one that catches people: the files land beside the native library, not in the working folder.
In Fahrenheit: with the SDK switched to Fahrenheit (SetTemperatureUnit() and its counterparts, see Temperatures in Fahrenheit), every temperature on this page is read and written in Fahrenheit, and every difference in Fahrenheit degrees; the samples are unchanged.
The palette chapter is four pages, each answering one question:
| Page | Question |
|---|---|
| Colour Palettes | Which palettes are there, how do I set one, and how do I read its colours? |
| Colour Range | Which temperatures do the colours span, and how is that chosen for me? |
| Custom Palettes | How do I supply a palette of my own? |
| The Palette Library | Where does the SDK look for palette files on this machine? |
On this page
The Palette Library
Operators add palettes by dropping a CSV file into a folder. That is how PIX Connect and SDK 11 have always worked, and it still works: the SDK scans four roots the first time your program asks the palette library for anything, and the library is how your program reaches what it found.
Listing what the machine has
#include <optrissdk.h> #include <filesystem> #include <string> // A path as UTF-8 text. path::string() is the ANSI code page on Windows, // which mangles a folder name that is not plain English and throws for // one that code page cannot hold, such as a Chinese user name. std::string Utf8(const std::filesystem::path &path) { const std::u8string text = path.u8string(); return std::string(text.begin(), text.end()); } void ListPalettes() { for (const optris::sdk::PaletteEntry &entry : optris::sdk::GetPaletteLibrary()) { const char *where = entry.source == optris::sdk::PaletteSource::kFile ? "from a file" : "built in"; std::println("{:<12} {:<12} {}", entry.name, where, Utf8(entry.path)); } }
Sorted by name, so a picker built straight from it is alphabetical. Never empty: the SDK's own palettes are always in it, so a picker cannot come out blank on a machine where nothing was installed.
The library has sixteen built-in entries, not seventeen. It is keyed by name, and kGrayscale and kGrayscaleBW share the name "GrayBW", so they are one row, and that row's entry.builtin is Palette::kGrayscaleBW, never kGrayscale. A picker built from GetAllPalettes() therefore has one more row than a picker built from GetPaletteLibrary(), and the two are showing the same set of colours. Build a picker from one or the other, not from both.
PaletteEntry::name is an identifier, unlike GetPaletteDisplayName(). It is the stem of the palette's file, it is stable, and it is what to persist when a palette has no enum value. When it has one, entry.builtin, persist that instead; it is one integer and it cannot go missing.
Using one
imager->SetCustomPalette(optris::sdk::GetPalette("Furnace"));
GetPalette() hands back a CustomPalette, because that is what a named table of colours is on this API. Everything in Supplying Your Own Palette applies to it, including ClearCustomPalette() to go back, and the fact that the colours are saved into the camera's configuration rather than a reference to them.
To ask without catching, use FindPalette():
if (const auto entry = optris::sdk::FindPalette(name)) { imager->SetCustomPalette(optris::sdk::GetPalette(entry->name)); } else { show_error(name + " is not one of the palettes on this machine"); }
Matching is exact and case-sensitive, like the filenames it comes from.
Where it looks
for (const auto &directory : optris::sdk::GetPaletteDirectories()) { const std::u8string text = directory.u8string(); // UTF-8, as above std::println("{}", std::string(text.begin(), text.end())); }
| Root | Linux | Windows |
|---|---|---|
| SDK | palettes in the SDK's data directory (see below) |
the same |
| Library | palettes beside libotcsdk.so |
palettes beside otcsdk-34.dll |
| Working | $PWD/palettes |
CD%\palettes |
| User | ~/.config/optris/palettes |
APPDATA%\Imager\palettes |
The SDK's data directory is the one that ships with the SDK library it loaded. When a Formats.def sits beside the library, it is the library's own folder, which is how the Python wheel and the NuGet package carry their data; otherwise it is the data directory of the installation the library was loaded from (share/otcsdk beside the library on Linux, the installation folder on Windows). Only a library with no data of its own, for example one linked statically into a program, takes it from $OTC_SDK_DIR, and failing that from the prefix the SDK was built for: /usr/share/otcsdk for the Debian package. So an SDK program reads its own palettes wherever it was installed, whatever other software has set OTC_SDK_DIR.
OTC_SDK_DIR may hold several directories, separated by ; on Windows and : on Linux, because the Windows installer adds one entry per installation. For a library that reads it, the SDK uses the first entry that names an existing directory, and when the variable holds more than one entry it logs which one it chose and which it passed over, so a machine with several SDK installations says which one is being read.
Later beats earlier, so a Furnace.csv in the user folder wins over one in the working folder. Worth putting on a diagnostics page: "my palette does not appear" is nearly always a file in a fourth folder.
The SDK installs a CSV for each of its own palettes: sixteen files, since kGrayscale and kGrayscaleBW share one. On a system install they land in the SDK folder; the NuGet package puts them beside the native library, which is the Library folder, not the working folder. That root exists for exactly this reason: a .NET application started by a .desktop entry, over SSH, or as a systemd service does not start in its own directory, so a working-folder lookup found none of the sixteen files and the SDK fell back to the compiled-in tables with a warning.
To search folders of your own instead, call SetPaletteDirectories({...}). It rescans immediately, follows the same rules, and an empty list goes back to the SDK's four.
Picking up a file added while you are running
The library is built the first time you ask it for anything and does not change on its own. Call RefreshPaletteLibrary() when a file may have appeared:
const size_t count = optris::sdk::RefreshPaletteLibrary();
Because nothing else ever rebuilds it, the order GetPaletteLibrary() reports is stable in between, which is what makes the C API's index-based enumeration safe.
What happens when something is wrong
| Situation | What the SDK does |
|---|---|
| A palette folder does not exist, or is empty | Nothing breaks. You get the full built-in set, and a warning in the log naming every folder searched. Never an empty list and never a grey picture. |
| A CSV is damaged, truncated or not a palette | Skipped, with the reason and the filename in the log. The files either side of it still load. One bad file costs that one palette. |
| Two folders hold the same name | Later wins. |
| A file is named after one of the SDK's own palettes | If its colours are the built-in's, it is that palette's shipped file and is recorded as entry.path. If they differ it is refused, with a warning naming the file, and the built-in is kept. |
That last row is deliberate. Palette::kIronBow is iron bow on every machine whatever is on its disk, and GetPalette("Iron") and GetPaletteColors(Palette::kIronBow) can never disagree. If overriding a built-in from a folder is something you need, say so; we would rather add it on purpose.
A program that never touches the filesystem sees none of this. The built-in tables are compiled into the library, not read from disk, so SetPalette(), GetPaletteColors() and GetAllPalettes() behave exactly as they always have and cannot fail because a file was missing.
From C
#include <optris/sdk_c.h> size_t count = 0; optris_sdk_palette_library_get_count(&count); for (size_t i = 0; i < count; ++i) { char name[128]; size_t size = 0; if (optris_sdk_palette_library_get_name(i, name, sizeof(name), &size) == OPTRIS_SDK_OK) { printf("%s\n", name); } } optris_sdk_imager_set_palette_by_name(imager, "Furnace");
optris_sdk_palette_library_get_info() gives the source and the optris_sdk_palette value when the name is one of the SDK's own; _get_path() gives the file; _read_colors() gives the table; _refresh() rescans; _get_directory_count() and _get_directory() list the folders that were searched.
From C#
foreach (PaletteInfo palette in PaletteLibrary.All) { Console.WriteLine($"{palette.Name} ({palette.Source}) {palette.Path}"); } camera.SetPaletteByName("Furnace"); camera.ClearCustomPalette(); // back to the built-in selection
PaletteLibrary.Find() returns null for a name the machine does not have rather than throwing; PaletteLibrary.GetColors() throws SdkObjectNotFoundException. PaletteLibrary.Directories lists the folders, and PaletteLibrary.Refresh() rescans them.
From Python
import optris_sdk for name in optris_sdk.library_names(): entry = optris_sdk.library_info(name) builtin = 'built-in' if entry.builtin is not None else 'file' print(name, builtin, optris_sdk.library_file(name)) # A name from the library, rather than one written in here: which # palettes a machine has depends on what is installed and what an # operator dropped in, so 'Furnace' may well not exist on yours. chosen = optris_sdk.library_names()[0] with optris_sdk.Camera.simulated() as camera: camera.set_palette_by_name(chosen) camera.clear_custom_palette() # back to the built-in selection optris_sdk.library_refresh() # rescan after a file was dropped in print(optris_sdk.library_directories())
library_info() and library_colors() both raise for a name the machine does not have; Python has no null-returning counterpart to PaletteLibrary.Find(). entry.builtin is the PALETTE_* constant when the name is one of the SDK's own and None when it is a file, which is what a caller persisting a number rather than a name needs.
library_file() returns '`, not an error, when the palette exists but no file was found for it. For a built-in that means an incomplete installation; the palette still works, because the compiled table does not depend on the file.
Every example in these four Python sections is exercised by PythonBindings.Palettes and PythonBindings.CustomPalettes in bindings/python/tests/test_binding.py, twenty tests in total. Python fences in this manual are not compiled by DocumentationCodeFences, so those tests are what stands behind them.
From Rust
use optris_sdk::palette::library; for name in library::names()? { let entry = library::info(&name)?; let kind = if entry.builtin.is_some() { "built-in" } else { "file" }; println!("{name} ({kind}) {}", library::file(&name)?); } // A name from the library, rather than one written in here: 'Furnace' // may well not exist on your machine. let chosen = &library::names()?[0]; camera.set_palette_by_name(chosen)?; camera.clear_custom_palette()?; // back to the built-in selection library::refresh()?; // rescan after a file was dropped in println!("{:?}", library::directories()?);
library::info() and library::colors() are an Err for a name the machine does not have, like Python and unlike PaletteLibrary.Find(). entry.builtin is Some(Palette) when the name is one of the SDK's own and None when it is a file, and library::file() returns an empty string when the palette exists with no file behind it, exactly as the other bindings report it.