Skip to content

Commit 242b92a

Browse files
committed
chore: add color support
1 parent e50e1b4 commit 242b92a

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
lines changed

src/filewriter.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ use std::io::Write;
1212
use std::io::{stdout, BufWriter, Cursor};
1313
use std::time;
1414

15+
pub fn get_color(bufferdata: BufferData) {
16+
let mut buff = Cursor::new(Vec::new());
17+
PngEncoder::new(&mut buff)
18+
.write_image(
19+
&bufferdata.frame_mmap.unwrap(),
20+
bufferdata.width,
21+
bufferdata.height,
22+
image::ColorType::Rgba8,
23+
)
24+
.unwrap();
25+
let image =
26+
image::load_from_memory_with_format(buff.get_ref(), image::ImageFormat::Png).unwrap();
27+
let pixel = image.get_pixel(0, 0);
28+
println!(
29+
"RGB: R:{}, G:{}, B:{}, A:{}",
30+
pixel.0[0], pixel.0[1], pixel.0[2], pixel[3]
31+
);
32+
println!(
33+
"16hex: #{:02x}{:02x}{:02x}{:02x}",
34+
pixel.0[0], pixel.0[1], pixel.0[2], pixel[3]
35+
);
36+
}
37+
1538
//use std::io::{stdout, BufWriter};
1639
pub fn write_to_file(bufferdata: BufferData, usestdout: bool) {
1740
if usestdout {

src/main.rs

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ impl AppData {
8484
None
8585
}
8686

87-
//fn get_pos_display_id(&self, pos: (i32, i32)) -> Option<usize> {
88-
// let (pos_x, pos_y) = pos;
89-
// for (i, ((width, height), (x, y))) in
90-
// zip(&self.display_logic_size, &self.display_postion).enumerate()
91-
// {
92-
// if pos_x >= *x && pos_x <= *x + *width && pos_y >= *y && pos_y <= *y + *height {
93-
// return Some(i);
94-
// }
95-
// }
96-
// None
97-
//}
87+
fn get_pos_display_id(&self, pos: (i32, i32)) -> Option<usize> {
88+
let (pos_x, pos_y) = pos;
89+
for (i, ((width, height), (x, y))) in
90+
zip(&self.display_logic_size, &self.display_position).enumerate()
91+
{
92+
if pos_x >= *x && pos_x <= *x + *width && pos_y >= *y && pos_y <= *y + *height {
93+
return Some(i);
94+
}
95+
}
96+
None
97+
}
9898

9999
fn get_pos_display_ids(&self, pos: (i32, i32), size: (i32, i32)) -> Option<Vec<usize>> {
100100
let (start_x, start_y) = pos;
@@ -190,11 +190,11 @@ impl AppData {
190190
),
191191
),
192192
) {
193-
println!("{}, {},", displayname, display_description);
194-
println!(" Size: {},{}", x, y);
195-
println!(" LogicSize: {}, {}", logic_x, logic_y);
196-
println!(" Position: {}, {}", pos_x, pos_y);
197-
println!(" Scale: {}", scale);
193+
println!("{displayname}, {display_description}");
194+
println!(" Size: {x},{y}");
195+
println!(" LogicSize: {logic_x}, {logic_y}");
196+
println!(" Position: {pos_x}, {pos_y}");
197+
println!(" Scale: {scale}");
198198
}
199199
}
200200
}
@@ -348,6 +348,10 @@ enum ClapOption {
348348
height: i32,
349349
usestdout: bool,
350350
},
351+
ShotWithColor {
352+
pos_x: i32,
353+
pos_y: i32,
354+
},
351355
}
352356

353357
enum SlurpParseResult {
@@ -461,6 +465,13 @@ fn main() {
461465
)
462466
.about("TakeScreenshot about whole screen"),
463467
)
468+
.subcommand(
469+
Command::new("color")
470+
.long_flag("color")
471+
.short_flag('C')
472+
.arg(arg!(<Point> ... "Pos by slurp"))
473+
.about("Get Color of a point"),
474+
)
464475
.subcommand(
465476
Command::new("list_outputs")
466477
.long_flag("list_outputs")
@@ -515,6 +526,17 @@ fn main() {
515526
}
516527
take_screenshot(ClapOption::ShotWithFullScreen { usestdout });
517528
}
529+
Some(("color", submatchs)) => {
530+
let posmessage = submatchs
531+
.get_one::<String>("Point")
532+
.expect("Need message")
533+
.to_string();
534+
let SlurpParseResult::Finished(pos_x, pos_y, _, _) = parseslurp(posmessage) else {
535+
return;
536+
};
537+
tracing_subscriber::fmt().init();
538+
take_screenshot(ClapOption::ShotWithColor { pos_x, pos_y })
539+
}
518540
#[cfg(feature = "gui")]
519541
Some(("gui", _)) => {
520542
tracing_subscriber::fmt::init();
@@ -720,6 +742,28 @@ fn take_screenshot(option: ClapOption) {
720742
}
721743
}
722744
}
745+
ClapOption::ShotWithColor { pos_x, pos_y } => {
746+
let xdg_output_manager = state.xdg_output_manager.clone().unwrap();
747+
for i in 0..state.displays.len() {
748+
xdg_output_manager.get_xdg_output(&state.displays[i], &qh, ());
749+
event_queue.roundtrip(&mut state).unwrap();
750+
}
751+
if let Some(id) = state.get_pos_display_id((pos_x, pos_y)) {
752+
let manager = state.wlr_screencopy.as_ref().unwrap();
753+
let shm = state.shm.clone().unwrap();
754+
if let Some(bufferdata) = wlrbackend::capture_output_frame(
755+
&conn,
756+
&state.displays[id],
757+
manager,
758+
&display,
759+
shm,
760+
(1, 1),
761+
Some((pos_x, pos_y, 1, 1)),
762+
) {
763+
filewriter::get_color(bufferdata);
764+
}
765+
}
766+
}
723767
ClapOption::ShowInfo => {
724768
let xdg_output_manager = state.xdg_output_manager.clone().unwrap();
725769
for i in 0..state.displays.len() {

0 commit comments

Comments
 (0)