summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Casper Storm <casper.storm@lich.io>2022-12-13 09:31:57 +0100
committerLibravatar Casper Storm <casper.storm@lich.io>2022-12-13 09:31:57 +0100
commit2e6d90f141217bad83eacd392562c13d7485881f (patch)
treebaa2c507076073aed4fd24abc9c7a7949d85c039 /core
parentba95042fff378213f5029b2b164d79e768482a47 (diff)
parent02182eea45537c9eb5b2bddfdff822bb8a3d143d (diff)
downloadiced-2e6d90f141217bad83eacd392562c13d7485881f.tar.gz
iced-2e6d90f141217bad83eacd392562c13d7485881f.tar.bz2
iced-2e6d90f141217bad83eacd392562c13d7485881f.zip
Merge branch 'master' into feat/slider-orientation
Diffstat (limited to '')
-rw-r--r--core/Cargo.toml2
-rw-r--r--core/src/color.rs21
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/padding.rs20
-rw-r--r--core/src/size.rs27
5 files changed, 63 insertions, 9 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml
index c9c7686e..c401f30a 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "iced_core"
-version = "0.5.0"
+version = "0.6.2"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2021"
description = "The essential concepts of Iced"
diff --git a/core/src/color.rs b/core/src/color.rs
index 212c1214..fe0a1856 100644
--- a/core/src/color.rs
+++ b/core/src/color.rs
@@ -89,6 +89,17 @@ impl Color {
}
}
+ /// Converts the [`Color`] into its RGBA8 equivalent.
+ #[must_use]
+ pub fn into_rgba8(self) -> [u8; 4] {
+ [
+ (self.r * 255.0).round() as u8,
+ (self.g * 255.0).round() as u8,
+ (self.b * 255.0).round() as u8,
+ (self.a * 255.0).round() as u8,
+ ]
+ }
+
/// Converts the [`Color`] into its linear values.
pub fn into_linear(self) -> [f32; 4] {
// As described in:
@@ -148,24 +159,26 @@ impl From<[f32; 4]> for Color {
#[macro_export]
macro_rules! color {
($r:expr, $g:expr, $b:expr) => {
- Color::from_rgb8($r, $g, $b)
+ $crate::Color::from_rgb8($r, $g, $b)
};
($r:expr, $g:expr, $b:expr, $a:expr) => {
- Color::from_rgba8($r, $g, $b, $a)
+ $crate::Color::from_rgba8($r, $g, $b, $a)
};
($hex:expr) => {{
let hex = $hex as u32;
let r = (hex & 0xff0000) >> 16;
let g = (hex & 0xff00) >> 8;
let b = (hex & 0xff);
- Color::from_rgb8(r as u8, g as u8, b as u8)
+
+ $crate::Color::from_rgb8(r as u8, g as u8, b as u8)
}};
($hex:expr, $a:expr) => {{
let hex = $hex as u32;
let r = (hex & 0xff0000) >> 16;
let g = (hex & 0xff00) >> 8;
let b = (hex & 0xff);
- Color::from_rgba8(r as u8, g as u8, b as u8, $a)
+
+ $crate::Color::from_rgba8(r as u8, g as u8, b as u8, $a)
}};
}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 03ba8cca..f95d61f6 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -7,7 +7,7 @@
//! ![The foundations of the Iced ecosystem](https://github.com/iced-rs/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/foundations.png?raw=true)
//!
//! [Iced]: https://github.com/iced-rs/iced
-//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.4/native
+//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native
//! [`iced_web`]: https://github.com/iced-rs/iced_web
#![doc(
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
diff --git a/core/src/padding.rs b/core/src/padding.rs
index 22467d6b..140ad8ee 100644
--- a/core/src/padding.rs
+++ b/core/src/padding.rs
@@ -1,3 +1,5 @@
+use crate::Size;
+
/// An amount of space to pad for each side of a box
///
/// You can leverage the `From` trait to build [`Padding`] conveniently:
@@ -71,9 +73,21 @@ impl Padding {
pub fn horizontal(self) -> u16 {
self.left + self.right
}
+
+ /// Fits the [`Padding`] between the provided `inner` and `outer` [`Size`].
+ pub fn fit(self, inner: Size, outer: Size) -> Self {
+ let available = (outer - inner).max(Size::ZERO);
+
+ Padding {
+ top: self.top.min((available.height as u16) / 2),
+ right: self.right.min((available.width as u16) / 2),
+ bottom: self.bottom.min((available.height as u16) / 2),
+ left: self.left.min((available.width as u16) / 2),
+ }
+ }
}
-impl std::convert::From<u16> for Padding {
+impl From<u16> for Padding {
fn from(p: u16) -> Self {
Padding {
top: p,
@@ -84,7 +98,7 @@ impl std::convert::From<u16> for Padding {
}
}
-impl std::convert::From<[u16; 2]> for Padding {
+impl From<[u16; 2]> for Padding {
fn from(p: [u16; 2]) -> Self {
Padding {
top: p[0],
@@ -95,7 +109,7 @@ impl std::convert::From<[u16; 2]> for Padding {
}
}
-impl std::convert::From<[u16; 4]> for Padding {
+impl From<[u16; 4]> for Padding {
fn from(p: [u16; 4]) -> Self {
Padding {
top: p[0],
diff --git a/core/src/size.rs b/core/src/size.rs
index 2db33a88..31f3171b 100644
--- a/core/src/size.rs
+++ b/core/src/size.rs
@@ -34,6 +34,22 @@ impl Size {
height: self.height + padding.vertical() as f32,
}
}
+
+ /// Returns the minimum of each component of this size and another
+ pub fn min(self, other: Self) -> Self {
+ Size {
+ width: self.width.min(other.width),
+ height: self.height.min(other.height),
+ }
+ }
+
+ /// Returns the maximum of each component of this size and another
+ pub fn max(self, other: Self) -> Self {
+ Size {
+ width: self.width.max(other.width),
+ height: self.height.max(other.height),
+ }
+ }
}
impl From<[f32; 2]> for Size {
@@ -68,3 +84,14 @@ impl From<Size> for Vector<f32> {
Vector::new(size.width, size.height)
}
}
+
+impl std::ops::Sub for Size {
+ type Output = Size;
+
+ fn sub(self, rhs: Self) -> Self::Output {
+ Size {
+ width: self.width - rhs.width,
+ height: self.height - rhs.height,
+ }
+ }
+}