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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use nappgui_sys::{
color_bgr, color_blue, color_get_alpha, color_get_rgb, color_get_rgba, color_get_rgbaf,
color_get_rgbf, color_gray, color_green, color_hsbf, color_html, color_red, color_rgb,
color_rgba, color_rgbaf, color_set_alpha, color_to_hsbf, color_to_html,
};
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
/// The colors in Draw2D are encoded using a 32-bit integer with the four RGBA channels in Little-Endian:
/// Red in byte 0, green in 1, blue in 2 and alpha (or transparency) in 3
pub struct Color {
pub(crate) inner: nappgui_sys::color_t,
}
impl Color {
pub(crate) fn new(color: nappgui_sys::color_t) -> Self {
Self { inner: color }
}
/// Create a color from the channels R (red), G (green) y B (blue).
pub fn rgb(r: u8, g: u8, b: u8) -> Self {
let color = unsafe { color_rgb(r, g, b) };
Self::new(color)
}
/// Create a color from the channels R (red), G (green), B (blue) and A (alpha).
pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
let color = unsafe { color_rgba(r, g, b, a) };
Self::new(color)
}
/// Create a color from the normalized RGBA channels from 0 to 1.
pub fn rgbaf(r: f32, g: f32, b: f32, a: f32) -> Self {
let color = unsafe { color_rgbaf(r, g, b, a) };
Self::new(color)
}
/// Creates a color (rgb) from its components Hue-Saturation-Brightness.
pub fn hsbf(h: f32, s: f32, b: f32) -> Self {
let color = unsafe { color_hsbf(h, s, b) };
Self::new(color)
}
/// Create an RGB color using only the red channel.
///
/// # Remarks
/// Equivalent to color_rgb(r, 0, 0).
pub fn red(r: u8) -> Self {
let color = unsafe { color_red(r) };
Self::new(color)
}
/// Create an RGB color using only the green channel.
///
/// # Remarks
/// Equivalent to color_rgb(0, g, 0).
pub fn green(g: u8) -> Self {
let color = unsafe { color_green(g) };
Self::new(color)
}
/// Create an RGB color using only the blue channel.
///
/// # Remarks
/// Equivalent to color_rgb(0, 0, b).
pub fn blue(b: u8) -> Self {
let color = unsafe { color_blue(b) };
Self::new(color)
}
/// Creates a gray RGB color from intensity value.
///
/// # Remarks
/// Equivalent to color_rgb(l, l, l).
pub fn gray(l: u8) -> Self {
let color = unsafe { color_gray(l) };
Self::new(color)
}
/// Create a color from a 32-bit BGR value. Byte 0 corresponds to channel B,
/// 1 to G and 2 to R. The highest order byte is ignored (set to 255).
///
/// # Remarks
/// This byte order is typical in Web colors.
pub fn bgr(bgr: u32) -> Self {
let color = unsafe { color_bgr(bgr) };
Self::new(color)
}
/// Create a color from a string in HTML or CSS format.
///
/// # Remarks
/// The color transformed to RGB.
pub fn html(html: &str) -> Self {
let html = std::ffi::CString::new(html).unwrap();
let color = unsafe { color_html(html.as_ptr()) };
Color::new(color)
}
/// Convert a color (rgb) to HSB space (hue, saturation, brightness).
pub fn to_hsbf(&self) -> (f32, f32, f32) {
let mut h = 0f32;
let mut s = 0f32;
let mut b = 0f32;
unsafe {
color_to_hsbf(self.inner, &mut h, &mut s, &mut b);
}
(h, s, b)
}
/// Convert a color to the HTML or CSS format (#RRGGBB).
pub fn to_html(&self) -> String {
const MAX_LEN: u32 = 32;
let mut html: Vec<i8> = Vec::with_capacity(MAX_LEN as usize);
unsafe {
color_to_html(self.inner, html.as_mut_ptr(), MAX_LEN);
std::ffi::CStr::from_ptr(html.as_ptr())
.to_string_lossy()
.into_owned()
}
}
/// Returns RGB color values.
///
/// # Remarks
/// In system or indexed colors, it makes effective the RGB value.
pub fn get_rgb(&self) -> (u8, u8, u8) {
let mut r = 0u8;
let mut g = 0u8;
let mut b = 0u8;
unsafe { color_get_rgb(self.inner, &mut r, &mut g, &mut b) }
(r, g, b)
}
/// Returns RGB color values, normalized from 0 to 1.
///
/// # Remarks
/// In system or indexed colors, it makes effective the RGB value.
pub fn get_rgbf(&self) -> (f32, f32, f32) {
let mut r = 0f32;
let mut g = 0f32;
let mut b = 0f32;
unsafe {
color_get_rgbf(self.inner, &mut r, &mut g, &mut b);
}
(r, g, b)
}
/// Returns the RGBA values of the color.
///
/// # Remarks
/// In system or indexed colors, it makes effective the RGBA value.
pub fn get_rgba(&self) -> (u8, u8, u8, u8) {
let mut r = 0u8;
let mut g = 0u8;
let mut b = 0u8;
let mut a = 0u8;
unsafe {
color_get_rgba(self.inner, &mut r, &mut g, &mut b, &mut a);
}
(r, g, b, a)
}
/// Returns the RGBA values of the color, normalized from 0 to 1.
///
/// # Remarks
/// In system or indexed colors, it makes effective the RGBA value.
pub fn get_rgbaf(&self) -> (f32, f32, f32, f32) {
let mut r = 0f32;
let mut g = 0f32;
let mut b = 0f32;
let mut a = 0f32;
unsafe {
color_get_rgbaf(self.inner, &mut r, &mut g, &mut b, &mut a);
}
(r, g, b, a)
}
/// Get the alpha (transparency) color component.
///
/// # Remarks
/// The alpha component. If it is equal 0 it means that the color is indexed
/// (does not contain RGB values).
pub fn get_alpha(&self) -> u8 {
unsafe { color_get_alpha(self.inner) }
}
/// Changes the alpha (transparency) value of a color.
///
/// # Remarks
/// The new color, with the altered alpha component.
pub fn set_alpha(&mut self, alpha: u8) {
let color = unsafe { color_set_alpha(self.inner, alpha) };
self.inner = color
}
}