Skip to content

Commit

Permalink
Merge pull request #1 from PixelDoted/graphs
Browse files Browse the repository at this point in the history
Graphs
  • Loading branch information
PixelDoted authored Jun 12, 2024
2 parents 96d6ead + c7301ea commit 05805f6
Show file tree
Hide file tree
Showing 17 changed files with 751 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
license = "GPL-3.0"

[dependencies]
bytemuck = { version = "1.16.0", features = ["derive"] }
i18n-embed-fl = "0.8"
log = "0.4.21"
once_cell = "1.19.0"
Expand All @@ -13,7 +14,6 @@ rust-embed = "8.3.0"

[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic.git"
rev = "2fc4184"
default-features = false
features = ["dbus-config", "tokio", "winit", "wgpu"]

Expand Down
1 change: 1 addition & 0 deletions i18n/en/cosmic_color_picker.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ app-title = COSMIC Color Picker
## Menu
view = View
graphs = Graphs
menu-about = About
## About
Expand Down
71 changes: 58 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use log::info;
pub struct ColorPicker {
pub spaces: Vec<ColorSpace>,
last_edited: usize,
show_graphs: bool,

colorspace_combo: widget::combo_box::State<ColorSpaceCombo>,
keybinds: HashMap<menu::KeyBind, Action>,
core: Core,
}

Expand All @@ -36,6 +38,7 @@ pub enum Message {
AddSpace,
RemoveSpace(usize),

ToggleGraphs,
ToggleAboutPage,
LaunchUrl(String),

Expand All @@ -45,6 +48,7 @@ pub enum Message {

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action {
ToggleGraphs,
About,
}

Expand All @@ -53,6 +57,7 @@ impl MenuAction for Action {

fn message(&self) -> Message {
match self {
Action::ToggleGraphs => Message::ToggleGraphs,
Action::About => Message::ToggleAboutPage,
}
}
Expand All @@ -79,8 +84,11 @@ impl Application for ColorPicker {
vec![MenuBar::new(vec![menu::Tree::with_children(
menu::root(fl!("view")),
menu::items(
&HashMap::new(),
vec![menu::Item::Button(fl!("menu-about"), Action::About)],
&self.keybinds,
vec![
menu::Item::CheckBox(fl!("graphs"), self.show_graphs, Action::ToggleGraphs),
menu::Item::Button(fl!("menu-about"), Action::About),
],
),
)])
.into()]
Expand All @@ -91,9 +99,19 @@ impl Application for ColorPicker {
}

fn init(core: Core, _flags: Self::Flags) -> (Self, Command<Self::Message>) {
let mut keybinds = HashMap::new();
keybinds.insert(
menu::KeyBind {
modifiers: vec![menu::key_bind::Modifier::Ctrl],
key: Key::Character("g".into()),
},
Action::ToggleGraphs,
);

let mut app = ColorPicker {
spaces: vec![ColorSpace::default()],
last_edited: 0,
show_graphs: false,

colorspace_combo: widget::combo_box::State::new(vec![
ColorSpaceCombo::Rgb,
Expand All @@ -102,6 +120,7 @@ impl Application for ColorPicker {
ColorSpaceCombo::Oklch,
ColorSpaceCombo::Cmyk,
]),
keybinds,
core,
};

Expand Down Expand Up @@ -135,6 +154,9 @@ impl Application for ColorPicker {
self.spaces.remove(index);
}

Message::ToggleGraphs => {
self.show_graphs = !self.show_graphs;
}
Message::ToggleAboutPage => {
self.core.window.show_context = !self.core.window.show_context;
}
Expand All @@ -149,6 +171,12 @@ impl Application for ColorPicker {
return self.copy_to_clipboard(index);
}
Message::Key(key, modifiers) => {
for (key_bind, action) in self.keybinds.iter() {
if key_bind.matches(modifiers, &key) {
return self.update(action.message());
}
}

if modifiers.control() && key == Key::Character("c".into()) {
return self.copy_to_clipboard(self.last_edited);
}
Expand All @@ -163,11 +191,31 @@ impl Application for ColorPicker {

for (colorspace, index) in self.spaces.iter().zip(0..) {
let (rgb, content, combo_selection) = match colorspace {
ColorSpace::Rgb(rgb) => (rgb.to_rgb(), rgb.view(), ColorSpaceCombo::Rgb),
ColorSpace::Hsv(hsv) => (hsv.to_rgb(), hsv.view(), ColorSpaceCombo::Hsv),
ColorSpace::Oklab(oklab) => (oklab.to_rgb(), oklab.view(), ColorSpaceCombo::Oklab),
ColorSpace::Oklch(oklch) => (oklch.to_rgb(), oklch.view(), ColorSpaceCombo::Oklch),
ColorSpace::Cmyk(cmyk) => (cmyk.to_rgb(), cmyk.view(), ColorSpaceCombo::Cmyk),
ColorSpace::Rgb(rgb) => (
rgb.to_rgb(),
rgb.view(self.show_graphs),
ColorSpaceCombo::Rgb,
),
ColorSpace::Hsv(hsv) => (
hsv.to_rgb(),
hsv.view(self.show_graphs),
ColorSpaceCombo::Hsv,
),
ColorSpace::Oklab(oklab) => (
oklab.to_rgb(),
oklab.view(self.show_graphs),
ColorSpaceCombo::Oklab,
),
ColorSpace::Oklch(oklch) => (
oklch.to_rgb(),
oklch.view(self.show_graphs),
ColorSpaceCombo::Oklch,
),
ColorSpace::Cmyk(cmyk) => (
cmyk.to_rgb(),
cmyk.view(self.show_graphs),
ColorSpaceCombo::Cmyk,
),
};

let min_rgb = rgb[0].min(rgb[1]).min(rgb[2]).min(0.0);
Expand Down Expand Up @@ -222,14 +270,11 @@ impl Application for ColorPicker {
contents = contents.push(widget::container(
widget::column::with_capacity(2)
.push(sidebar)
.push(
content
.map(move |message| Message::ColorSpace { index, message })
.apply(widget::scrollable),
)
.push(content.map(move |message| Message::ColorSpace { index, message }))
.spacing(10.0)
.padding(10.0)
.width(300.0),
.width(300.0)
.apply(widget::scrollable),
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/colorspace/cmyk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Cmyk {
self.strings[index] = string;
}

pub fn view<'a>(&self) -> cosmic::Element<'a, Message> {
pub fn view<'a>(&self, _show_graphs: bool) -> cosmic::Element<'a, Message> {
let values = &self.values;
let strings = &self.strings;

Expand Down
27 changes: 24 additions & 3 deletions src/colorspace/hsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use cosmic::{
widget,
};

use crate::{colorspace::ColorSpaceMessage as Message, fl, widgets::color_slider};
use crate::{
colorspace::ColorSpaceMessage as Message, fl, shaders::hsv as shader, widgets::color_slider,
};

const COLOR_STOPS_HUE: [ColorStop; 7] = [
ColorStop {
Expand Down Expand Up @@ -97,7 +99,7 @@ impl Hsv {
self.strings[index] = string;
}

pub fn view<'a>(&self) -> cosmic::Element<'a, Message> {
pub fn view<'a>(&self, show_graphs: bool) -> cosmic::Element<'a, Message> {
let values = &self.values;
let strings = &self.strings;

Expand Down Expand Up @@ -159,12 +161,30 @@ impl Hsv {
.spacing(10.0)
.padding(10.0);

let content = widget::column::with_capacity(3)
let mut content = widget::column::with_capacity(3)
.push(widget::container(red).style(cosmic::style::Container::Card))
.push(widget::container(green).style(cosmic::style::Container::Card))
.push(widget::container(blue).style(cosmic::style::Container::Card))
.spacing(10.0);

if show_graphs {
content = content.push(
widget::container(
widget::container(
cosmic::iced_widget::shader(shader::ColorGraph {
hue: self.values[0],
saturation: self.values[1],
value: self.values[2],
})
.width(100)
.height(100),
)
.padding(10.0),
)
.style(cosmic::style::Container::Card),
);
}

content.into()
}
}
Expand Down Expand Up @@ -234,6 +254,7 @@ fn rgb_to_hsv(r: f32, g: f32, b: f32) -> [f32; 3] {
[h, s, x_max]
}

// ---- Tests ----
#[cfg(test)]
mod test {
use super::{hsv_to_rgb, rgb_to_hsv};
Expand Down
41 changes: 35 additions & 6 deletions src/colorspace/oklab.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: GPL-3.0-only

use cosmic::{
iced::{gradient::ColorStop, Alignment, Color},
iced::{gradient::ColorStop, Alignment, Color, Length},
widget,
};

use crate::{colorspace::ColorSpaceMessage as Message, fl, widgets::color_slider};
use crate::{
colorspace::ColorSpaceMessage as Message, fl, shaders::oklab as shader, widgets::color_slider,
};

const COLOR_STOPS_LIGHTNESS: [ColorStop; 2] = [
ColorStop {
Expand Down Expand Up @@ -77,11 +79,11 @@ impl Oklab {
self.strings[index] = string;
}

pub fn view<'a>(&self) -> cosmic::Element<'a, Message> {
pub fn view<'a>(&self, show_graphs: bool) -> cosmic::Element<'a, Message> {
let values = &self.values;
let strings = &self.strings;

let lightness = widget::column::with_capacity(3)
let mut lightness = widget::column::with_capacity(3)
.push(
widget::row::with_capacity(2)
.push(widget::text(fl!("lightness")).size(20.0))
Expand All @@ -100,7 +102,7 @@ impl Oklab {
))
.spacing(10.0)
.padding(10.0);
let green_red = widget::column::with_capacity(3)
let mut green_red = widget::column::with_capacity(3)
.push(
widget::row::with_capacity(2)
.push(widget::text(fl!("green-red")).size(20.0))
Expand All @@ -119,7 +121,7 @@ impl Oklab {
))
.spacing(10.0)
.padding(10.0);
let blue_yellow = widget::column::with_capacity(3)
let mut blue_yellow = widget::column::with_capacity(3)
.push(
widget::row::with_capacity(2)
.push(widget::text(fl!("blue-yellow")).size(20.0))
Expand All @@ -139,6 +141,33 @@ impl Oklab {
.spacing(10.0)
.padding(10.0);

if show_graphs {
lightness = lightness.push(
cosmic::iced_widget::shader(shader::ColorGraph::<0> {
lightness: self.values[0],
green_red: self.values[1],
blue_yellow: self.values[2],
})
.width(Length::Fill),
);
green_red = green_red.push(
cosmic::iced_widget::shader(shader::ColorGraph::<1> {
lightness: self.values[0],
green_red: self.values[1],
blue_yellow: self.values[2],
})
.width(Length::Fill),
);
blue_yellow = blue_yellow.push(
cosmic::iced_widget::shader(shader::ColorGraph::<2> {
lightness: self.values[0],
green_red: self.values[1],
blue_yellow: self.values[2],
})
.width(Length::Fill),
);
}

let content = widget::column::with_capacity(3)
.push(widget::container(lightness).style(cosmic::style::Container::Card))
.push(widget::container(green_red).style(cosmic::style::Container::Card))
Expand Down
Loading

0 comments on commit 05805f6

Please sign in to comment.