Skip to content

Commit

Permalink
chore: add color support
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Jan 31, 2023
1 parent e50e1b4 commit 242b92a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 16 deletions.
23 changes: 23 additions & 0 deletions src/filewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ use std::io::Write;
use std::io::{stdout, BufWriter, Cursor};
use std::time;

pub fn get_color(bufferdata: BufferData) {
let mut buff = Cursor::new(Vec::new());
PngEncoder::new(&mut buff)
.write_image(
&bufferdata.frame_mmap.unwrap(),
bufferdata.width,
bufferdata.height,
image::ColorType::Rgba8,
)
.unwrap();
let image =
image::load_from_memory_with_format(buff.get_ref(), image::ImageFormat::Png).unwrap();
let pixel = image.get_pixel(0, 0);
println!(
"RGB: R:{}, G:{}, B:{}, A:{}",
pixel.0[0], pixel.0[1], pixel.0[2], pixel[3]
);
println!(
"16hex: #{:02x}{:02x}{:02x}{:02x}",
pixel.0[0], pixel.0[1], pixel.0[2], pixel[3]
);
}

//use std::io::{stdout, BufWriter};
pub fn write_to_file(bufferdata: BufferData, usestdout: bool) {
if usestdout {
Expand Down
76 changes: 60 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ impl AppData {
None
}

//fn get_pos_display_id(&self, pos: (i32, i32)) -> Option<usize> {
// let (pos_x, pos_y) = pos;
// for (i, ((width, height), (x, y))) in
// zip(&self.display_logic_size, &self.display_postion).enumerate()
// {
// if pos_x >= *x && pos_x <= *x + *width && pos_y >= *y && pos_y <= *y + *height {
// return Some(i);
// }
// }
// None
//}
fn get_pos_display_id(&self, pos: (i32, i32)) -> Option<usize> {
let (pos_x, pos_y) = pos;
for (i, ((width, height), (x, y))) in
zip(&self.display_logic_size, &self.display_position).enumerate()
{
if pos_x >= *x && pos_x <= *x + *width && pos_y >= *y && pos_y <= *y + *height {
return Some(i);
}
}
None
}

fn get_pos_display_ids(&self, pos: (i32, i32), size: (i32, i32)) -> Option<Vec<usize>> {
let (start_x, start_y) = pos;
Expand Down Expand Up @@ -190,11 +190,11 @@ impl AppData {
),
),
) {
println!("{}, {},", displayname, display_description);
println!(" Size: {},{}", x, y);
println!(" LogicSize: {}, {}", logic_x, logic_y);
println!(" Position: {}, {}", pos_x, pos_y);
println!(" Scale: {}", scale);
println!("{displayname}, {display_description}");
println!(" Size: {x},{y}");
println!(" LogicSize: {logic_x}, {logic_y}");
println!(" Position: {pos_x}, {pos_y}");
println!(" Scale: {scale}");
}
}
}
Expand Down Expand Up @@ -348,6 +348,10 @@ enum ClapOption {
height: i32,
usestdout: bool,
},
ShotWithColor {
pos_x: i32,
pos_y: i32,
},
}

enum SlurpParseResult {
Expand Down Expand Up @@ -461,6 +465,13 @@ fn main() {
)
.about("TakeScreenshot about whole screen"),
)
.subcommand(
Command::new("color")
.long_flag("color")
.short_flag('C')
.arg(arg!(<Point> ... "Pos by slurp"))
.about("Get Color of a point"),
)
.subcommand(
Command::new("list_outputs")
.long_flag("list_outputs")
Expand Down Expand Up @@ -515,6 +526,17 @@ fn main() {
}
take_screenshot(ClapOption::ShotWithFullScreen { usestdout });
}
Some(("color", submatchs)) => {
let posmessage = submatchs
.get_one::<String>("Point")
.expect("Need message")
.to_string();
let SlurpParseResult::Finished(pos_x, pos_y, _, _) = parseslurp(posmessage) else {
return;
};
tracing_subscriber::fmt().init();
take_screenshot(ClapOption::ShotWithColor { pos_x, pos_y })
}
#[cfg(feature = "gui")]
Some(("gui", _)) => {
tracing_subscriber::fmt::init();
Expand Down Expand Up @@ -720,6 +742,28 @@ fn take_screenshot(option: ClapOption) {
}
}
}
ClapOption::ShotWithColor { pos_x, pos_y } => {
let xdg_output_manager = state.xdg_output_manager.clone().unwrap();
for i in 0..state.displays.len() {
xdg_output_manager.get_xdg_output(&state.displays[i], &qh, ());
event_queue.roundtrip(&mut state).unwrap();
}
if let Some(id) = state.get_pos_display_id((pos_x, pos_y)) {
let manager = state.wlr_screencopy.as_ref().unwrap();
let shm = state.shm.clone().unwrap();
if let Some(bufferdata) = wlrbackend::capture_output_frame(
&conn,
&state.displays[id],
manager,
&display,
shm,
(1, 1),
Some((pos_x, pos_y, 1, 1)),
) {
filewriter::get_color(bufferdata);
}
}
}
ClapOption::ShowInfo => {
let xdg_output_manager = state.xdg_output_manager.clone().unwrap();
for i in 0..state.displays.len() {
Expand Down

0 comments on commit 242b92a

Please sign in to comment.