1
2
3
4
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
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
#[allow(warnings)]
#[cfg(feature = "screenshot")]
pub mod screenshot {
use image::{GenericImageView, ImageBuffer, Rgba, RgbaImage};
use crate::images;
/// Captures screenshots of all available screens and saves them as PNG files.
///
/// The screenshots are saved with filenames in the format `screenshot_{index}.png`,
/// where `index` corresponds to the screen number.
///
/// # Examples
///
/// ```
/// use doe::screenshot;
/// screenshot::capture_all();
/// ```
pub fn capture_all() {
use screenshots::Screen;
let screens = Screen::all().unwrap();
for (index, screen) in screens.iter().enumerate() {
let mut image = screen.capture().unwrap();
image.save(format!("screenshot_{}.png", index)).unwrap();
}
}
/// Crops a specific area from an image.
///
/// # Arguments
///
/// * `img` - The source image as an `ImageBuffer<Rgba<u8>, Vec<u8>>`.
/// * `x` - The x-coordinate of the top-left corner of the area to crop.
/// * `y` - The y-coordinate of the top-left corner of the area to crop.
/// * `width` - The width of the area to crop.
/// * `height` - The height of the area to crop.
///
/// # Returns
///
/// Returns `Some(RgbaImage)` containing the cropped area if successful.
/// Returns `None` if the cropping operation fails.
///
/// # Examples
///
/// ```
/// use doe::screenshot;
/// use image::{ImageBuffer, Rgba};
///
/// let img = ImageBuffer::new(100, 100);
/// let cropped = screenshot::cut_area(img, 10, 10, 50, 50).unwrap();
/// ```
pub fn cut_area(img: ImageBuffer<Rgba<u8>, Vec<u8>>, x: u32, y: u32, width: u32, height: u32) -> Option<RgbaImage> {
let sub_image = img.view(x, y, width, height);
let mut cut_image = RgbaImage::new(width, height);
for (x, y, pixel) in sub_image.pixels() {
cut_image.put_pixel(x, y, pixel);
}
Some(cut_image)
}
/// Captures a specific area from the primary screen.
///
/// # Arguments
///
/// * `x` - The x-coordinate of the top-left corner of the area to capture.
/// * `y` - The y-coordinate of the top-left corner of the area to capture.
/// * `width` - The width of the area to capture.
/// * `height` - The height of the area to capture.
///
/// # Returns
///
/// Returns `Some(RgbaImage)` containing the captured area if successful.
/// Returns `None` if the capture operation fails.
///
/// # Examples
///
/// ```
/// use doe::screenshot;
///
/// let captured = screenshot::capture_area(10, 10, 50, 50).unwrap();
/// ```
pub fn capture_area(x: u32, y: u32, width: u32, height: u32) -> Option<RgbaImage> {
use screenshots::Screen;
let screens = Screen::all().unwrap();
if let Some(screen) = screens.iter().nth(0) {
let image = screen.capture().unwrap();
let new_image = cut_area(image, x, y, width, height);
return new_image;
} else {
return None;
}
}
/// Retrieves the RGB color value from a specific position in an image.
///
/// # Arguments
///
/// * `image_path` - The path to the image file.
/// * `x` - The x-coordinate of the pixel to retrieve.
/// * `y` - The y-coordinate of the pixel to retrieve.
///
/// # Returns
///
/// Returns `Some((u8, u8, u8))` containing the RGB values if the position is valid.
/// Returns `None` if the position is outside the image bounds.
///
/// # Examples
///
/// ```
/// use doe::screenshot;
///
/// let rgb = screenshot::get_rgb_from_position("screenshot_0.png", 10, 10).unwrap();
/// println!("RGB: {:?}", rgb);
/// ```
pub fn get_rgb_from_position(image_path: &str, x: u32, y: u32) -> Option<(u8, u8, u8)> {
use image::{GenericImageView, ImageBuffer, Rgb};
let image = image::open(image_path).expect("Failed to open image");
let width = image.width();
let height = image.height();
if x < width && y < height {
let pixel = image.get_pixel(x, y);
let r = pixel[0];
let g = pixel[1];
let b = pixel[2];
let a = pixel[3];
return Some((r, g, b));
} else {
return None;
}
}
/// Checks if the color at a specific screen position matches a given hex color.
///
/// # Arguments
///
/// * `position` - A tuple `(u32, u32)` representing the screen position to check.
/// * `hex` - A string slice representing the hex color to compare against.
///
/// # Returns
///
/// Returns `true` if the color at the specified position matches the hex color.
/// Returns `false` otherwise.
///
/// # Examples
///
/// ```
/// use doe::screenshot;
///
/// let is_color_match = screenshot::screen_position_is((10, 10), "#FFFFFF");
/// println!("Color match: {}", is_color_match);
/// ```
pub fn screen_position_is(position: (u32, u32), hex: &str) -> bool {
capture_all();
if crate::color::color::rgb_to_hex(
get_rgb_from_position("screenshot_0.png", position.0, position.1).unwrap(),
) == hex
{
true
} else {
false
}
}
}
#[cfg(feature = "screenshot")]
pub use screenshot::*;