use crate::layer::{Layer, LayerId};
use std::any::Any;
#[derive(Debug, Clone)]
pub struct BackgroundLayer {
id: LayerId,
name: String,
visible: bool,
opacity: f32,
color: [f32; 4],
}
impl BackgroundLayer {
pub fn new(name: impl Into<String>, color: [f32; 4]) -> Self {
Self {
id: LayerId::next(),
name: name.into(),
visible: true,
opacity: 1.0,
color,
}
}
#[inline]
pub fn color(&self) -> [f32; 4] {
self.color
}
pub fn set_color(&mut self, color: [f32; 4]) {
self.color = color;
}
#[inline]
pub fn effective_color(&self) -> [f32; 4] {
[
self.color[0],
self.color[1],
self.color[2],
self.color[3] * self.opacity,
]
}
}
impl Layer for BackgroundLayer {
fn id(&self) -> LayerId {
self.id
}
fn kind(&self) -> crate::layer::LayerKind {
crate::layer::LayerKind::Background
}
fn name(&self) -> &str {
&self.name
}
fn visible(&self) -> bool {
self.visible
}
fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
fn opacity(&self) -> f32 {
self.opacity
}
fn set_opacity(&mut self, opacity: f32) {
self.opacity = opacity.clamp(0.0, 1.0);
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::Layer;
#[test]
fn effective_color_applies_opacity() {
let mut layer = BackgroundLayer::new("bg", [0.2, 0.3, 0.4, 1.0]);
layer.set_opacity(0.5);
assert_eq!(layer.effective_color(), [0.2, 0.3, 0.4, 0.5]);
}
#[test]
fn kind_is_background() {
let layer = BackgroundLayer::new("bg", [0.0, 0.0, 0.0, 1.0]);
assert_eq!(layer.kind(), crate::layer::LayerKind::Background);
}
}