Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use palette for colors #94

Draft
wants to merge 21 commits into
base: v0.8-pre
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Documentation on [docs.rs](https://docs.rs/dessin/0.8.2-pre/)

```rust
use dessin::prelude::*;
use palette::{named, Srgb};

#[derive(Default, Shape)]
struct MyShape {
Expand All @@ -34,7 +35,7 @@ impl MyShape {
}
impl From<MyShape> for Shape {
fn from(MyShape { text }: MyShape) -> Self {
dessin2!(Text!(fill = Color::RED, { text })).into()
dessin2!(Text!(fill = Srgb::<f32>::from_format(named::RED).into_linear(), { text })).into()
}
}

Expand All @@ -44,11 +45,11 @@ fn main() {

dessin2!([
Circle!(
fill = Color::RED,
fill = Srgb::<f32>::from_format(named::RED).into_linear(),
{ radius },
translate = [x as f32 * 5., 10.],
),
Text!(fill = Color::BLACK, font_size = 10., text = "Hi !",),
Text!(fill = Srgb::<f32>::from_format(named::BLACK).into_linear(), font_size = 10., text = "Hi !",),
])
});

Expand Down
32 changes: 26 additions & 6 deletions dessin-image/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ impl Exporter for ImageExporter {

let style = self.style();

if let Some(Fill::Color(c)) = style.fill {
let (r, g, b, a) = c.rgba();
if let Some(color) = style.fill {
let (r, g, b, a) = (
color.into_format::<u8, f32>().red,
color.into_format::<u8, f32>().green,
color.into_format::<u8, f32>().blue,
color.into_format::<u8, u8>().alpha,
);
self.buffer.fill(
&path,
&Source::Solid(SolidSource { r: b, g, b: r, a }),
Expand All @@ -178,7 +183,12 @@ impl Exporter for ImageExporter {

match style.stroke {
Some(Stroke::Full { color, width }) => {
let (r, g, b, a) = color.rgba();
let (r, g, b, a) = (
color.into_format::<u8, f32>().red,
color.into_format::<u8, f32>().green,
color.into_format::<u8, f32>().blue,
color.into_format::<u8, u8>().alpha,
);
self.buffer.stroke(
&path,
&Source::Solid(SolidSource { r: b, g, b: r, a }),
Expand All @@ -199,7 +209,12 @@ impl Exporter for ImageExporter {
on,
off,
}) => {
let (r, g, b, a) = color.rgba();
let (r, g, b, a) = (
color.into_format::<u8, f32>().red,
color.into_format::<u8, f32>().green,
color.into_format::<u8, f32>().blue,
color.into_format::<u8, u8>().alpha,
);
self.buffer.stroke(
&path,
&Source::Solid(SolidSource { r: b, g, b: r, a }),
Expand Down Expand Up @@ -241,10 +256,15 @@ impl Exporter for ImageExporter {
// dt.set_transform(&Transform::rotation(euclid::Angle::degrees(15.0)));

let color = match self.style().fill {
Some(Fill::Color(c)) => c,
Some(color) => color,
None => return Ok(()),
};
let (r, g, b, a) = color.rgba();
let (r, g, b, a) = (
color.into_format::<u8, f32>().red, //-----------------------------------------------------------------------------
color.into_format::<u8, f32>().green, //before : color.rgba();
color.into_format::<u8, f32>().blue, //rgba() modification should be better
color.into_format::<u8, u8>().alpha, //-----------------------------------------------------------------------------
);

let font = font_kit::loader::Loader::from_bytes(std::sync::Arc::new(font.to_vec()), 0)
.map_err(|e| ImageError::FontLoadingError(e))?;
Expand Down
28 changes: 23 additions & 5 deletions dessin-pdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use dessin::{
};
use nalgebra::Translation2;
use printpdf::{
BuiltinFont, IndirectFontRef, Line, Mm, PdfDocument, PdfDocumentReference, PdfLayerReference,
Point,
color, BuiltinFont, IndirectFontRef, Line, Mm, PdfDocument, PdfDocumentReference,
PdfLayerReference, Point,
};
use std::{collections::HashMap, fmt};

Expand Down Expand Up @@ -62,7 +62,11 @@ impl Exporter for PDFExporter {
) -> Result<(), Self::Error> {
if let Some(fill) = fill {
let (r, g, b) = match fill {
Fill::Color(c) => c.as_rgb_f32(),
color => (
color.into_format::<f32, f32>().red,
color.into_format::<f32, f32>().green,
color.into_format::<f32, f32>().blue,
),
};

self.layer
Expand All @@ -76,7 +80,14 @@ impl Exporter for PDFExporter {

if let Some(stroke) = stroke {
let ((r, g, b), w) = match stroke {
Stroke::Full { color, width } => (color.as_rgb_f32(), width),
Stroke::Full { color, width } => (
(
color.into_format::<f32, f32>().red,
color.into_format::<f32, f32>().green,
color.into_format::<f32, f32>().blue,
),
width,
),
Stroke::Dashed {
color,
width,
Expand All @@ -93,7 +104,14 @@ impl Exporter for PDFExporter {
gap_3: None,
});

(color.as_rgb_f32(), width)
(
(
color.into_format::<f32, f32>().red,
color.into_format::<f32, f32>().green,
color.into_format::<f32, f32>().blue,
),
width,
)
}
};

Expand Down
23 changes: 20 additions & 3 deletions dessin-svg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ impl SVGExporter {

fn write_style(&mut self, style: StylePosition) -> Result<(), SVGError> {
match style.fill {
Some(Fill::Color(color)) => write!(self.acc, "fill='{color}' ")?,
Some(color) => write!(
self.acc,
"fill='rgb({} {} {} / {:.3})' ",
(color.red * 255.) as u32,
(color.green * 255.) as u32,
(color.blue * 255.) as u32,
color.alpha
)?, // pass [0;1] number to [0;255] for a working CSS code (not needed for alpha)

None => write!(self.acc, "fill='none' ")?,
}

Expand All @@ -88,10 +96,19 @@ impl SVGExporter {
off,
}) => write!(
self.acc,
"stroke='{color}' stroke-width='{width}' stroke-dasharray='{on},{off}' "
"stroke='rgb({} {} {} / {:.3})' stroke-width='{width}' stroke-dasharray='{on},{off}' ",
(color.red * 255.) as u32,
(color.green * 255.) as u32,
(color.blue * 255.) as u32,
color.alpha
)?,
Some(Stroke::Full { color, width }) => {
write!(self.acc, "stroke='{color}' stroke-width='{width}' ")?
write!(self.acc, "stroke='rgb({} {} {} / {:.3})' stroke-width='{width}' ",
(color.red * 255.) as u32,
(color.green * 255.) as u32,
(color.blue * 255.) as u32,
color.alpha
)?
}

None => {}
Expand Down
2 changes: 1 addition & 1 deletion dessin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dessin-macros = { path = "../dessin-macros", version = "0.8.20-pre" }
fontdue = "^0.8.0"
image = "^0.24.8"
nalgebra = "^0.32.3"
# palette = "^0.7.2"
palette = "^0.7.5"

[dev-dependencies]
assert_float_eq = "1.1.3"
8 changes: 4 additions & 4 deletions dessin/src/contrib/layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use nalgebra::Transform2;

use crate::prelude::*;
use nalgebra::Transform2;

/// Display children on top of one another
#[derive(Debug, Default, Clone, Shape)]
Expand Down Expand Up @@ -257,14 +256,15 @@ mod tests {
assert_eq!(bb.height(), 2. + height_triangle);
}

use palette::{named, Srgb};
#[test]
fn layout_of_textbox() {
let text = "test\nwhy\nnot";
let gap = 2.;

let first_text = dessin2!(TextBox!(
{ text },
fill = Fill::Color(Color::BLACK),
fill = Srgb::<f32>::from_format(named::BLACK).into_linear(),
font_size = 3.6,
align = TextAlign::Left,
width = 115.,
Expand All @@ -278,7 +278,7 @@ mod tests {
{ first_text }(),
Text!(
text = "Notes",
fill = Color::BLACK,
fill = Srgb::<f32>::from_format(named::BLACK).into_linear(),
font_weight = FontWeight::Bold,
font_size = 3.6,
align = TextAlign::Left,
Expand Down
1 change: 1 addition & 0 deletions dessin/src/contrib/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone, PartialEq, Shape)]
pub struct Padding<T> {
#[shape(into)]
#[shape(into)]
pub shape: T,

Expand Down
5 changes: 3 additions & 2 deletions dessin/src/contrib/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl From<TextBox> for Shape {
}
}

use palette::{named, Srgb};
#[test]
fn one_line() {
use assert_float_eq::*;
Expand All @@ -170,7 +171,7 @@ fn one_line() {
let shape: Shape = dessin2!(
TextBox!(
{ text },
fill = Fill::Color(Color::BLACK),
fill = Srgb::<f32>::from_format(named::BLACK).into_linear(),
font_size = 5.,
align = TextAlign::Left,
line_spacing = 2.,
Expand All @@ -189,7 +190,7 @@ fn two_lines() {

let shape: Shape = dessin2!(TextBox!(
{ text },
fill = Fill::Color(Color::BLACK),
fill = Srgb::<f32>::from_format(named::BLACK).into_linear(),
font_size = 5.,
align = TextAlign::Left,
line_spacing = 2.,
Expand Down
8 changes: 5 additions & 3 deletions dessin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//!
//! ```
//! use dessin::prelude::*;
//! use palette::{named, Srgb};
//!
//! #[derive(Default, Shape)]
//! struct MyShape {
Expand All @@ -18,7 +19,7 @@
//! }
//! impl From<MyShape> for Shape {
//! fn from(MyShape { text }: MyShape) -> Self {
//! dessin2!(Text!(fill = Color::RED, { text })).into()
//! dessin2!(Text!(fill = Srgb::<f32>::from_format(named::RED).into_linear(), { text })).into()
//! }
//! }
//!
Expand All @@ -28,11 +29,11 @@
//!
//! dessin2!([
//! Circle!(
//! fill = Color::RED,
//! fill = Srgb::<f32>::from_format(named::RED).into_linear(),
//! { radius },
//! translate = [x as f32 * 5., 10.],
//! ),
//! Text!(fill = Color::BLACK, font_size = 10., text = "Hi !",),
//! Text!(fill = Srgb::<f32>::from_format(named::BLACK).into_linear(), font_size = 10., text = "Hi !",),
//! ])
//! });
//!
Expand Down Expand Up @@ -172,6 +173,7 @@ pub mod style;

pub use ::image;
pub use ::nalgebra;
pub use ::palette;

/// Prelude module includes everyting you need to build a dessin.
/// You can of courses cherry pick what you need by importing directly from other modules.
Expand Down
3 changes: 2 additions & 1 deletion dessin/src/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub use dynamic::*;
pub use ellipse::*;
use na::{Point2, Rotation2, Scale2, Vector2};
use nalgebra::{self as na, Transform2, Translation2};
use palette::{IntoColor, Srgb, Srgba};
use std::{fmt, marker::PhantomData, sync::Arc};
pub use text::*;

Expand Down Expand Up @@ -511,7 +512,7 @@ pub enum Shape {
/// Block of style
Style {
/// Fill
fill: Option<crate::style::Fill>,
fill: Option<Srgba>,
/// Stroke
stroke: Option<crate::style::Stroke>,
/// Styled shape. (Or Shapes if it is a [`Groupe`][Shape::Group])
Expand Down
Loading
Loading