feat: 选择功能支持透明模式
This commit is contained in:
@@ -270,13 +270,21 @@ impl MSCanvas {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_rect(&self, x: i32, y: i32, w: i32, h: i32) -> RectSelection {
|
||||
pub fn select_rect(&self, x: i32, y: i32, w: i32, h: i32, is_transparent_white: bool) -> RectSelection {
|
||||
let mut result = vec![255; (w * h * 4) as usize];
|
||||
|
||||
let mut index = 0;
|
||||
let default_color = if is_transparent_white {
|
||||
MSColor::ZERO
|
||||
} else {
|
||||
MSColor::WHITE
|
||||
};
|
||||
for yi in y..(y + h) {
|
||||
for xi in x..(x + w) {
|
||||
let color = self.pixel_at(xi, yi).unwrap_or(MSColor::WHITE);
|
||||
let mut color = self.pixel_at(xi, yi).unwrap_or(default_color);
|
||||
if is_transparent_white && color == MSColor::WHITE {
|
||||
color = MSColor::ZERO;
|
||||
}
|
||||
result[index] = color.r;
|
||||
result[index + 1] = color.g;
|
||||
result[index + 2] = color.b;
|
||||
|
||||
13
src/paint.rs
13
src/paint.rs
@@ -4,7 +4,7 @@ use iced::padding;
|
||||
use iced::time::{self, Instant, milliseconds};
|
||||
use iced::widget::canvas::Canvas;
|
||||
use iced::widget::container;
|
||||
use iced::widget::{Column, button, column, image, pick_list, row, text};
|
||||
use iced::widget::{Column, button, column, image, pick_list, row, text, checkbox};
|
||||
use iced::{Border, Element, Length, Point, Renderer, Subscription, Task};
|
||||
use iced::{color, event, mouse};
|
||||
use std::thread;
|
||||
@@ -111,6 +111,8 @@ enum Message {
|
||||
ClickForegroundColor(MSColor),
|
||||
ClickBackgroundColor(MSColor),
|
||||
SwapForeBackColor,
|
||||
|
||||
CheckboxToggled(bool),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -229,6 +231,7 @@ struct Config {
|
||||
rounded_radius: i32,
|
||||
brush_kind: Option<BrushKind>,
|
||||
shape_style: Option<ShapeStyle>,
|
||||
is_transparent_white: bool,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
@@ -246,6 +249,7 @@ impl Config {
|
||||
rounded_radius: 3,
|
||||
brush_kind: None,
|
||||
shape_style: None,
|
||||
is_transparent_white: false,
|
||||
};
|
||||
|
||||
fn incr(&mut self, option: ConfigOption, step: i32) {
|
||||
@@ -581,6 +585,9 @@ impl PaintApp {
|
||||
Message::ShapeStyleSelected,
|
||||
)
|
||||
.placeholder("Shape Style..."),
|
||||
checkbox(self.config.is_transparent_white)
|
||||
.label("Transparent White")
|
||||
.on_toggle(Message::CheckboxToggled),
|
||||
];
|
||||
debug_area = debug_area.padding(padding::top(10).left(5).bottom(10));
|
||||
|
||||
@@ -709,6 +716,9 @@ impl PaintApp {
|
||||
self.canvas.stroke_color(self.foreground_color);
|
||||
self.canvas.fill_color(self.foreground_color);
|
||||
}
|
||||
Message::CheckboxToggled(is_checked) => {
|
||||
self.config.is_transparent_white = is_checked;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -859,6 +869,7 @@ impl PaintApp {
|
||||
y as i32,
|
||||
width as i32,
|
||||
height as i32,
|
||||
self.config.is_transparent_white,
|
||||
));
|
||||
self.canvas
|
||||
.clear_rect(x as i32, y as i32, width as i32, height as i32);
|
||||
|
||||
Reference in New Issue
Block a user