Color picker
Picks a colour from a palette, a saturation and value square with hue and alpha, or a hex value.
Theme colour
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.strong("Accent colour");
ui.weak("Used for primary actions and highlights.");
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.add(
ColorPicker::new(&mut state.accent)
.open(&mut state.open)
.id_salt("accent"),
);
});
});
let (rect, _) = ui
.allocate_exact_size(egui::vec2(ui.available_width(), 96.0), egui::Sense::hover());
ui.painter().rect_filled(rect, 8.0, state.accent);
ui.monospace(format!("{:?}", state.accent));
Colour select
ui.add(
ColorPicker::optional(&mut state.select)
.field()
.id_salt("select"),
);
ui.add(
ColorPicker::optional(&mut state.empty)
.field()
.placeholder("Pick a colour")
.id_salt("empty"),
);
Sizes and labels
for (name, size) in [
("Extra small", Size::XSmall),
("Small", Size::Small),
("Medium", Size::Medium),
("Large", Size::Large),
] {
ui.add(
ColorPicker::new(&mut state.sized)
.size(size)
.label(name)
.id_salt(name),
);
}
Featured colours
let brand = [
egui::Color32::from_rgb(0x0f, 0x17, 0x2a),
egui::Color32::from_rgb(0xe1, 0x1d, 0x48),
egui::Color32::from_rgb(0xf5, 0x9e, 0x0b),
egui::Color32::from_rgb(0x10, 0xb9, 0x81),
];
ui.add(
ColorPicker::new(&mut state.accent)
.featured_colors(brand)
.no_alpha()
.label("Brand colour")
.id_salt("brand"),
);
ui.add(
ColorPicker::new(&mut state.sized)
.label("Disabled")
.disabled(true)
.id_salt("disabled"),
);
The story above is gallery/src/stories/color_picker.rs, running in your browser. Every snippet on this page is cut from it.