1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::tables::Layer;
/// Represents an indexed color.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Color {
raw_value: i16,
}
impl Color {
/// Returns `true` if the color defaults back to the item's layer's color.
pub fn is_by_layer(&self) -> bool {
self.raw_value == 256
}
/// Returns `true` if the color defaults back to the entity's color.
pub fn is_by_entity(&self) -> bool {
self.raw_value == 257
}
/// Returns `true` if the color defaults back to the containing block's color.
pub fn is_by_block(&self) -> bool {
self.raw_value == 0
}
/// Returns `true` if the color represents a `Layer` that is turned off.
pub fn is_turned_off(&self) -> bool {
self.raw_value < 0
}
/// Sets the color to default back to the item's layer's color.
pub fn set_by_layer(&mut self) {
self.raw_value = 256
}
/// Sets the color to default back to the containing block's color.
pub fn set_by_block(&mut self) {
self.raw_value = 0
}
/// Sets the color to default back to the containing entity's color.
pub fn set_by_entity(&mut self) {
self.raw_value = 257
}
/// Sets the color to represent a `Layer` that is turned off.
pub fn turn_off(&mut self) {
self.raw_value = -1
}
/// Returns `true` if the color represents a proper color index.
pub fn is_index(&self) -> bool {
self.raw_value >= 1 && self.raw_value <= 255
}
/// Gets an `Option<u8>` of the indexable value of the color.
pub fn index(&self) -> Option<u8> {
if self.is_index() {
Some(self.raw_value as u8)
} else {
None
}
}
pub(crate) fn raw_value(&self) -> i16 {
self.raw_value
}
pub(crate) fn from_raw_value(val: i16) -> Color {
Color { raw_value: val }
}
/// Creates a `Color` that defaults to the item's layer's color.
pub fn by_layer() -> Color {
Color { raw_value: 256 }
}
/// Creates a `Color` that defaults back to the containing block's color.
pub fn by_block() -> Color {
Color { raw_value: 0 }
}
/// Creates a `Color` that defaults back to the containing entity's color.
pub fn by_entity() -> Color {
Color { raw_value: 257 }
}
/// Creates a `Color` from the specified index.
pub fn from_index(i: u8) -> Color {
Color {
raw_value: i16::from(i),
}
}
pub(crate) fn writable_color_value(&self, layer: &Layer) -> i16 {
let value = self.raw_value().abs();
if layer.is_layer_on {
value
} else {
-value
}
}
}