Function fltk::draw::set_draw_color
source · pub fn set_draw_color(color: Color)
Expand description
Sets the drawing color
Examples found in repository?
examples/table.rs (line 56)
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
fn draw_header(txt: &str, x: i32, y: i32, w: i32, h: i32) {
draw::push_clip(x, y, w, h);
draw::draw_box(
enums::FrameType::ThinUpBox,
x,
y,
w,
h,
enums::Color::FrameDefault,
);
draw::set_draw_color(enums::Color::Black);
draw::set_font(enums::Font::Helvetica, 14);
draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
draw::pop_clip();
}
// The selected flag sets the color of the cell to a grayish color, otherwise white
fn draw_data(txt: &str, x: i32, y: i32, w: i32, h: i32, selected: bool) {
draw::push_clip(x, y, w, h);
if selected {
draw::set_draw_color(enums::Color::from_u32(0x00D3_D3D3));
} else {
draw::set_draw_color(enums::Color::White);
}
draw::draw_rectf(x, y, w, h);
draw::set_draw_color(enums::Color::Gray0);
draw::set_font(enums::Font::Helvetica, 14);
draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
draw::draw_rect(x, y, w, h);
draw::pop_clip();
}
More examples
examples/shapedwindow_taskbar.rs (line 122)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
fn prep_shape(w: i32, h: i32) -> image::RgbImage {
let surf = surface::ImageSurface::new(w, h, false);
surface::ImageSurface::push_current(&surf);
draw::set_draw_color(enums::Color::Black);
draw::draw_rectf(-1, -1, w + 2, h + 2);
draw::set_draw_color(enums::Color::White);
draw::draw_rounded_rectf(0, 0, w, h, 16);
let img = surf.image().unwrap();
surface::ImageSurface::pop_current();
img
}
examples/spreadsheet.rs (line 122)
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
fn draw_header(txt: &str, x: i32, y: i32, w: i32, h: i32) {
draw::push_clip(x, y, w, h);
draw::draw_box(
enums::FrameType::ThinUpBox,
x,
y,
w,
h,
enums::Color::FrameDefault,
);
draw::set_draw_color(enums::Color::Black);
draw::set_font(enums::Font::Helvetica, 14);
draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
draw::pop_clip();
}
// The selected flag sets the color of the cell to a grayish color, otherwise white
fn draw_data(txt: &str, x: i32, y: i32, w: i32, h: i32, selected: bool) {
draw::push_clip(x, y, w, h);
if selected {
draw::set_draw_color(enums::Color::from_u32(0x00D3_D3D3));
} else {
draw::set_draw_color(enums::Color::White);
}
draw::draw_rectf(x, y, w, h);
draw::set_draw_color(enums::Color::Gray0);
draw::set_font(enums::Font::Helvetica, 14);
draw::draw_text2(txt, x, y, w, h, enums::Align::Center);
draw::draw_rect(x, y, w, h);
draw::pop_clip();
}
examples/custom_widgets.rs (line 78)
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
fn draw(&mut self) {
self.wid.draw(move |b| {
draw::draw_box(
FrameType::FlatBox,
b.x(),
b.y(),
b.width(),
b.height(),
Color::from_u32(0x304FFE),
);
draw::set_draw_color(Color::White);
draw::set_font(Font::Courier, 24);
draw::draw_text2(
&b.label(),
b.x(),
b.y(),
b.width(),
b.height(),
Align::Center,
);
});
}
// Overrides the handle function.
// Notice the do_callback which allows the set_callback method to work
fn handle(&mut self) {
let mut wid = self.wid.clone();
self.wid.handle(move |_, ev| match ev {
Event::Push => {
wid.do_callback();
true
}
_ => false,
});
}
}
impl Deref for FlatButton {
type Target = Widget;
fn deref(&self) -> &Self::Target {
&self.wid
}
}
impl DerefMut for FlatButton {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.wid
}
}
pub struct PowerButton {
frm: Frame,
on: Rc<RefCell<bool>>,
}
impl PowerButton {
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Self {
let mut frm = Frame::new(x, y, w, h, "");
frm.set_frame(FrameType::FlatBox);
frm.set_color(Color::Black);
let on = Rc::from(RefCell::from(false));
frm.draw({
// storing two almost identical images here, in a real application this could be optimized
let on = Rc::clone(&on);
let mut on_svg =
SvgImage::from_data(&POWER.to_string().replace("red", "green")).unwrap();
on_svg.scale(frm.width(), frm.height(), true, true);
let mut off_svg = SvgImage::from_data(POWER).unwrap();
off_svg.scale(frm.width(), frm.height(), true, true);
move |f| {
if *on.borrow() {
on_svg.draw(f.x(), f.y(), f.width(), f.height());
} else {
off_svg.draw(f.x(), f.y(), f.width(), f.height());
};
}
});
frm.handle({
let on = on.clone();
move |f, ev| match ev {
Event::Push => {
let prev = *on.borrow();
*on.borrow_mut() = !prev;
f.do_callback();
f.redraw();
true
}
_ => false,
}
});
Self { frm, on }
}
pub fn is_on(&self) -> bool {
*self.on.borrow()
}
}
impl Deref for PowerButton {
type Target = Frame;
fn deref(&self) -> &Self::Target {
&self.frm
}
}
impl DerefMut for PowerButton {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.frm
}
}
pub struct FancyHorSlider {
s: Slider,
}
impl FancyHorSlider {
pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
let mut s = Slider::new(x, y, width, height, "");
s.set_type(SliderType::Horizontal);
s.set_frame(FrameType::RFlatBox);
s.set_color(Color::from_u32(0x868db1));
s.draw(|s| {
draw::set_draw_color(Color::Blue);
draw::draw_pie(
s.x() - 10 + (s.w() as f64 * s.value()) as i32,
s.y() - 10,
30,
30,
0.,
360.,
);
});
Self { s }
}
}
impl Deref for FancyHorSlider {
type Target = Slider;
fn deref(&self) -> &Self::Target {
&self.s
}
}
impl DerefMut for FancyHorSlider {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.s
}
}
fn main() {
let app = app::App::default().with_scheme(app::Scheme::Gtk);
// app::set_visible_focus(false);
let mut wind = Window::default()
.with_size(800, 600)
.with_label("Custom Widgets");
let mut but = FlatButton::new(350, 350, 160, 80, "Increment");
let mut power = PowerButton::new(600, 100, 100, 100);
let mut dial = FillDial::new(100, 100, 200, 200, "0");
let mut frame = Frame::default()
.with_size(160, 80)
.with_label("0")
.above_of(&*but, 20);
let mut fancy_slider = FancyHorSlider::new(100, 550, 500, 10);
let mut toggle = button::ToggleButton::new(650, 400, 80, 35, "@+9circle")
.with_align(Align::Left | Align::Inside);
wind.end();
wind.show();
wind.set_color(Color::Black);
frame.set_label_size(32);
frame.set_label_color(Color::from_u32(0xFFC300));
dial.set_label_color(Color::White);
dial.set_label_font(Font::CourierBold);
dial.set_label_size(24);
dial.set_color(Color::from_u32(0x6D4C41));
dial.set_color(Color::White);
dial.set_selection_color(Color::Red);
toggle.set_frame(FrameType::RFlatBox);
toggle.set_label_color(Color::White);
toggle.set_selection_color(Color::from_u32(0x00008B));
toggle.set_color(Color::from_u32(0x585858));
toggle.clear_visible_focus();
toggle.set_callback(|t| {
if t.is_set() {
t.set_align(Align::Right | Align::Inside);
} else {
t.set_align(Align::Left | Align::Inside);
}
t.parent().unwrap().redraw();
});
dial.draw(|d| {
draw::set_draw_color(Color::Black);
draw::draw_pie(d.x() + 20, d.y() + 20, 160, 160, 0., 360.);
draw::draw_pie(d.x() - 5, d.y() - 5, 210, 210, -135., -45.);
});
dial.set_callback(|d| {
d.set_label(&format!("{}", (d.value() * 100.) as i32));
app::redraw();
});
but.set_callback(move |_| {
frame.set_label(&(frame.label().parse::<i32>().unwrap() + 1).to_string())
});
power.set_callback(move |_| {
println!("power button clicked");
});
fancy_slider.set_callback(|s| s.parent().unwrap().redraw());
app.run().unwrap();
}
examples/gradients.rs (line 19)
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
fn create_vertical_gradient_frame(
x: i32,
y: i32,
w: i32,
h: i32,
col1: Color,
col2: Color,
) -> frame::Frame {
let mut frame = frame::Frame::new(x, y, w, h, "Vertical");
frame.draw(move |f| {
let imax = f.h();
let d = if imax > 0 { imax } else { 1 };
for i in 0..=imax {
let w = 1.0 - i as f32 / d as f32;
set_draw_color(Color::inactive(&Color::color_average(col1, col2, w)));
draw_xyline(f.x(), f.y() + i, f.x() + f.w());
}
set_draw_color(Color::Black);
set_font(Font::Helvetica, app::font_size());
draw_text2(&f.label(), f.x(), f.y(), f.w(), f.h(), f.align());
});
frame
}
fn create_horizontal_gradient_frame(
x: i32,
y: i32,
w: i32,
h: i32,
col1: Color,
col2: Color,
) -> frame::Frame {
let mut frame = frame::Frame::new(x, y, w, h, "Horizontal");
frame.draw(move |f| {
let imax = f.w();
let d = if imax > 0 { imax } else { 1 };
for i in 0..=imax {
let w = 1.0 - i as f32 / d as f32;
set_draw_color(Color::inactive(&Color::color_average(col1, col2, w)));
draw_yxline(f.x() + i, f.y(), f.y() + f.h());
}
set_draw_color(Color::Black);
set_font(Font::Helvetica, app::font_size());
draw_text2(&f.label(), f.x(), f.y(), f.w(), f.h(), f.align());
});
frame
}
fn create_horizontal_svg_gradient_frame(
x: i32,
y: i32,
w: i32,
h: i32,
col1: Color,
col2: Color,
) -> frame::Frame {
let mut frame = frame::Frame::new(x, y, w, h, "Svg");
frame.draw(move |f| {
let (r1, g1, b1) = Color::inactive(&col1).to_rgb();
let (r2, g2, b2) = Color::inactive(&col2).to_rgb();
let svg = format!(
"<svg viewBox='0 0 {} {}'>
<defs>
<linearGradient id='grad1' x1='0%' y1='0%' x2='0%' y2='100%'>
<stop offset='0%' style='stop-color:rgb({},{},{});stop-opacity:1' />
<stop offset='100%' style='stop-color:rgb({},{},{});stop-opacity:1' />
</linearGradient>
</defs>
<rect width='100%' height='100%' fill='url(#grad1)' />
</svg>",
f.w(),
f.h() + 1,
r1,
g1,
b1,
r2,
g2,
b2
);
let mut image = image::SvgImage::from_data(&svg).unwrap();
image.draw(f.x(), f.y(), f.w(), f.h());
set_draw_color(Color::Black);
set_font(Font::Helvetica, app::font_size());
draw_text2(&f.label(), f.x(), f.y(), f.w(), f.h(), f.align());
});
frame
}
fn main() {
let a = app::App::default();
let mut win = window::Window::default().with_size(300, 300);
create_vertical_gradient_frame(0, 0, 100, 100, Color::Red, Color::Cyan);
create_horizontal_gradient_frame(100, 0, 100, 100, Color::Red, Color::Cyan);
create_horizontal_svg_gradient_frame(200, 0, 100, 100, Color::Red, Color::Cyan);
win.end();
win.draw(|w| {
// vertical gradient
let imax = w.w();
let d = if imax > 0 { imax } else { 1 };
for i in 0..=imax {
let v = 1.0 - i as f32 / d as f32;
set_draw_color(Color::color_average(Color::Red, Color::Blue, v));
draw_yxline(i, 0, w.h());
}
w.draw_children();
});
win.make_resizable(true);
win.show();
a.run().unwrap();
}
examples/custom_dial.rs (line 35)
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
pub fn new(x: i32, y: i32, w: i32, h: i32, label: &str) -> Self {
let value = Rc::from(RefCell::from(0));
let mut main_wid = group::Group::new(x, y, w, h, None)
.with_label(label)
.with_align(Align::Top);
let mut value_frame =
frame::Frame::new(main_wid.x(), main_wid.y() + 80, main_wid.w(), 40, "0");
value_frame.set_label_size(26);
main_wid.end();
let value_c = value.clone();
main_wid.draw(move |w| {
draw::set_draw_rgb_color(230, 230, 230);
draw::draw_pie(w.x(), w.y(), w.w(), w.h(), 0., 180.);
draw::set_draw_hex_color(0xb0bf1a);
draw::draw_pie(
w.x(),
w.y(),
w.w(),
w.h(),
(100 - *value_c.borrow()) as f64 * 1.8,
180.,
);
draw::set_draw_color(Color::White);
draw::draw_pie(
w.x() - 50 + w.w() / 2,
w.y() - 50 + w.h() / 2,
100,
100,
0.,
360.,
);
w.draw_children();
});
Self {
main_wid,
value,
value_frame,
}
}
Additional examples can be found in: