summaryrefslogtreecommitdiffstats
path: root/core/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-02-20 05:51:18 +0100
committerLibravatar GitHub <noreply@github.com>2020-02-20 05:51:18 +0100
commit17271eae671a933a862dc85aa5b9956a7da70b28 (patch)
tree1a24e3eebb544b0bc8b0ebf2d92f330141f699bf /core/src
parent8d63c49ba1aa43407e0dab0a8e69d3f316a79279 (diff)
parent6f7247ca13181bcdfe1a3065215c1b3204723b84 (diff)
downloadiced-17271eae671a933a862dc85aa5b9956a7da70b28.tar.gz
iced-17271eae671a933a862dc85aa5b9956a7da70b28.tar.bz2
iced-17271eae671a933a862dc85aa5b9956a7da70b28.zip
Merge pull request #193 from hecrj/feature/canvas
Canvas widget for 2D graphics
Diffstat (limited to 'core/src')
-rw-r--r--core/src/color.rs9
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/point.rs7
-rw-r--r--core/src/size.rs51
4 files changed, 67 insertions, 2 deletions
diff --git a/core/src/color.rs b/core/src/color.rs
index d6bdd365..db509b88 100644
--- a/core/src/color.rs
+++ b/core/src/color.rs
@@ -44,11 +44,18 @@ impl Color {
///
/// [`Color`]: struct.Color.html
pub fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
+ Color::from_rgba8(r, g, b, 1.0)
+ }
+
+ /// Creates a [`Color`] from its RGB8 components and an alpha value.
+ ///
+ /// [`Color`]: struct.Color.html
+ pub fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
Color {
r: f32::from(r) / 255.0,
g: f32::from(g) / 255.0,
b: f32::from(b) / 255.0,
- a: 1.0,
+ a,
}
}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 3cbce743..ea5e8b43 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -22,6 +22,7 @@ mod font;
mod length;
mod point;
mod rectangle;
+mod size;
mod vector;
pub use align::{Align, HorizontalAlignment, VerticalAlignment};
@@ -31,4 +32,5 @@ pub use font::Font;
pub use length::Length;
pub use point::Point;
pub use rectangle::Rectangle;
+pub use size::Size;
pub use vector::Vector;
diff --git a/core/src/point.rs b/core/src/point.rs
index 47c8b142..b9a8149c 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -11,10 +11,15 @@ pub struct Point {
}
impl Point {
+ /// The origin (i.e. a [`Point`] with both X=0 and Y=0).
+ ///
+ /// [`Point`]: struct.Point.html
+ pub const ORIGIN: Point = Point::new(0.0, 0.0);
+
/// Creates a new [`Point`] with the given coordinates.
///
/// [`Point`]: struct.Point.html
- pub fn new(x: f32, y: f32) -> Self {
+ pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
diff --git a/core/src/size.rs b/core/src/size.rs
new file mode 100644
index 00000000..389b3247
--- /dev/null
+++ b/core/src/size.rs
@@ -0,0 +1,51 @@
+use std::f32;
+
+/// An amount of space in 2 dimensions.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct Size {
+ /// The width.
+ pub width: f32,
+ /// The height.
+ pub height: f32,
+}
+
+impl Size {
+ /// A [`Size`] with zero width and height.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub const ZERO: Size = Size::new(0., 0.);
+
+ /// A [`Size`] with infinite width and height.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY);
+
+ /// A [`Size`] of infinite width and height.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub const fn new(width: f32, height: f32) -> Self {
+ Size { width, height }
+ }
+
+ /// Increments the [`Size`] to account for the given padding.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub fn pad(&self, padding: f32) -> Self {
+ Size {
+ width: self.width + padding * 2.0,
+ height: self.height + padding * 2.0,
+ }
+ }
+}
+
+impl From<[f32; 2]> for Size {
+ fn from([width, height]: [f32; 2]) -> Self {
+ Size { width, height }
+ }
+}
+
+impl From<[u16; 2]> for Size {
+ fn from([width, height]: [u16; 2]) -> Self {
+ Size::new(width.into(), height.into())
+ }
+}