use std::path::Path;
use crate::{path_to_utf8, sys, Error, Result};
pub struct ColorConfig {
inner: cxx::UniquePtr<sys::color::ColorConfig>,
}
unsafe impl Send for ColorConfig {}
impl ColorConfig {
pub fn new() -> Result<Self> {
Self::from_inner(sys::color::colorconfig_default())
}
pub fn from_path(config_path: &Path) -> Result<Self> {
let filename = path_to_utf8(config_path)?;
Self::from_inner(sys::color::colorconfig_from_file(filename))
}
pub fn supports_opencolorio() -> bool {
sys::color::colorconfig_supports_opencolorio()
}
pub fn name(&self) -> String {
sys::color::colorconfig_name(self.inner())
}
pub fn color_space_names(&self) -> Vec<String> {
sys::color::colorconfig_color_space_names(self.inner())
}
pub fn role_names(&self) -> Vec<String> {
sys::color::colorconfig_role_names(self.inner())
}
pub fn color_space_for_role(&self, role: &str) -> Option<String> {
let name = sys::color::colorconfig_color_space_for_role(self.inner(), role);
(!name.is_empty()).then_some(name)
}
pub fn has_color_space(&self, name: &str) -> bool {
sys::color::colorconfig_color_space_index(self.inner(), name) >= 0
}
fn from_inner(inner: cxx::UniquePtr<sys::color::ColorConfig>) -> Result<Self> {
if inner.is_null() {
return Err(Error::operation(
"open colour configuration",
"OpenImageIO returned no configuration".to_owned(),
));
}
let config = Self { inner };
let error = sys::color::colorconfig_geterror(config.inner());
if !error.is_empty() {
return Err(Error::operation("open colour configuration", error));
}
Ok(config)
}
fn inner(&self) -> &sys::color::ColorConfig {
self.inner
.as_ref()
.expect("ColorConfig invariant violated: null native pointer")
}
}
impl std::fmt::Debug for ColorConfig {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("ColorConfig")
.field("name", &self.name())
.field("color_spaces", &self.color_space_names().len())
.field("roles", &self.role_names().len())
.finish()
}
}