Skip to content

Commit

Permalink
Added screen color picking
Browse files Browse the repository at this point in the history
  • Loading branch information
PixelDoted committed Oct 3, 2024
1 parent 05805f6 commit e4ccb56
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ log = "0.4.21"
once_cell = "1.19.0"
open = "5.1.3"
rust-embed = "8.3.0"
ashpd = "0.9.1"

[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic.git"
Expand Down
38 changes: 36 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct ColorPicker {

#[derive(Debug, Clone)]
pub enum Message {
None,
ColorSpace {
index: usize,
message: ColorSpaceMessage,
Expand All @@ -43,6 +44,8 @@ pub enum Message {
LaunchUrl(String),

CopyToClipboard(usize),
PickScreenRequest(usize),
PickScreenResponse((usize, ashpd::desktop::Color)),
Key(Key, Modifiers),
}

Expand Down Expand Up @@ -130,6 +133,7 @@ impl Application for ColorPicker {

fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::None => (),
Message::ColorSpace { index: i, message } => match message {
ColorSpaceMessage::ChangeValue { index, value } => {
self.spaces[i].change_value(index, value);
Expand Down Expand Up @@ -170,6 +174,29 @@ impl Application for ColorPicker {
Message::CopyToClipboard(index) => {
return self.copy_to_clipboard(index);
}
Message::PickScreenRequest(index) => {
return cosmic::command::future(async move {
let req = ashpd::desktop::Color::pick().send().await;
let Ok(req) = req else {
println!("{req:?}");
return Message::None;
};

let res = req.response();
let Ok(color) = res else {
println!("{res:?}");
return Message::None;
};

println!("{index} {color:?}");

Message::PickScreenResponse((index, color))
});
}
Message::PickScreenResponse((index, color)) => {
let (r, g, b) = (color.red(), color.green(), color.blue());
self.spaces[index].from_rgb([r as f32, g as f32, b as f32])
}
Message::Key(key, modifiers) => {
for (key_bind, action) in self.keybinds.iter() {
if key_bind.matches(modifiers, &key) {
Expand Down Expand Up @@ -245,15 +272,22 @@ impl Application for ColorPicker {
widget::row::with_capacity(3)
.push(
widget::button::icon(widget::icon::from_name("edit-copy-symbolic"))
.on_press(Message::CopyToClipboard(index)),
.on_press(Message::CopyToClipboard(index))
.tooltip("Copy to Clipboard"),
)
.push(
widget::button::icon(widget::icon::from_name("edit-find-symbolic"))
.on_press(Message::PickScreenRequest(index))
.tooltip("Pick a color from the screen"),
)
.push(widget::Space::with_width(Length::Fill))
.push(
widget::button::icon(widget::icon::from_name(
"user-trash-full-symbolic",
))
.on_press(Message::RemoveSpace(index))
.style(theme::Button::Destructive),
.style(theme::Button::Destructive)
.tooltip("Delete"),
),
)
.push(widget::combo_box(
Expand Down
10 changes: 10 additions & 0 deletions src/colorspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ impl ColorSpace {
Self::Cmyk(Cmyk::from_rgb(rgb))
}

pub fn from_rgb(&mut self, rgb_in: [f32; 3]) {
match self {
ColorSpace::Rgb(rgb) => *rgb = rgb::Rgb::from_rgb(rgb_in),
ColorSpace::Hsv(hsv) => *hsv = hsv::Hsv::from_rgb(rgb_in),
ColorSpace::Oklab(oklab) => *oklab = oklab::Oklab::from_rgb(rgb_in),
ColorSpace::Oklch(oklch) => *oklch = oklch::Oklch::from_rgb(rgb_in),
ColorSpace::Cmyk(cmyk) => *cmyk = cmyk::Cmyk::from_rgb(rgb_in),
}
}

pub fn get_rgb(&self) -> [f32; 3] {
match self {
ColorSpace::Rgb(rgb) => rgb.to_rgb(),
Expand Down

0 comments on commit e4ccb56

Please sign in to comment.