Skip to content

Commit fbe2d1b

Browse files
committed
documentation fixes
1 parent a297019 commit fbe2d1b

4 files changed

Lines changed: 74 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Versions prior to v0.7 are not tagged/released on GitHub.
4848
- This allows you to create a gradient from an iterator of colors or an iterator of `(color, position)` pairs
4949
- Add `Image::from_fill`
5050
- Add `LoopCount::count_or`
51+
- Add `Rgb[a]::from_u32`
5152

5253
## v0.10.3 (2024-09-23)
5354
- Fix GIF encoding issues as produced in [#38](https://github.com/jay3332/ril/issues/38)

src/draw.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,12 @@ impl<F: IntoFill> Default for Rectangle<F> {
837837
impl<F: IntoFill> Rectangle<F> {
838838
/// Creates a new rectangle with default values.
839839
///
840-
/// This immediately sets the position to `(0, 0)` and you must explicitly set the size of the
841-
/// rectangle with [`with_size`][Self::with_size] in order to set a size for the rectangle.
840+
/// This immediately sets the position to `(0, 0)` and you must explicitly set the size of the
841+
/// rectangle with [`with_size`][Self::with_size] in order to set a size for the rectangle.
842842
/// If no size is set before drawing, you will receive a panic.
843843
///
844844
/// This also does not set any border or fill for the rectangle, you must explicitly set either
845-
/// one of them with [`with_fill`][Self::with_fill] or [`with_border`][Self::with_border]
845+
/// one of them with [`with_fill`][Self::with_fill] or [`with_border`][Self::with_border]
846846
/// respectively or else you will receive a panic at draw-time.
847847
#[must_use]
848848
#[deprecated = "use `Rectangle::at` instead"]
@@ -991,11 +991,11 @@ impl<F: IntoFill> Draw<F::Pixel> for Rectangle<F> {
991991
/// An ellipse, which could be a circle.
992992
///
993993
/// Using any of the predefined constructors will automatically set the position to `(0, 0)` and
994-
/// you must explicitly set the size of the ellipse with [`with_size`][Self::with_size] in order to
994+
/// you must explicitly set the size of the ellipse with [`with_size`][Self::with_size] in order to
995995
/// set a size for the ellipse. If no size is set before drawing, you will receive a panic.
996996
///
997997
/// This also does not set any border or fill for the ellipse, you must explicitly set either one
998-
/// of them with [`with_fill`][Self::with_fill] or [`with_border`][Self::with_border] respectively
998+
/// of them with [`with_fill`][Self::with_fill] or [`with_border`][Self::with_border] respectively
999999
/// or else you will receive a panic at draw-time.
10001000
#[derive(Clone, Debug)]
10011001
pub struct Ellipse<F: IntoFill> {
@@ -1031,10 +1031,10 @@ impl<F: IntoFill> Ellipse<F> {
10311031
/// of the ellipse. You should explicitly set the position of the center of the ellipse with
10321032
/// [`with_position`][Self::with_position] or else you will receive a panic at draw-time.
10331033
///
1034-
/// You must also specify a size for the ellipse with [`with_size`][Self::with_size] or else
1034+
/// You must also specify a size for the ellipse with [`with_size`][Self::with_size] or else
10351035
/// you will receive a panic at draw-time.
10361036
///
1037-
/// Finally, you must also specify a fill color with [`with_fill`][Self::with_fill] or a border
1037+
/// Finally, you must also specify a fill color with [`with_fill`][Self::with_fill] or a border
10381038
/// color with [`with_border`][Self::with_border] or else you will receive a panic at draw-time.
10391039
#[must_use]
10401040
pub fn new() -> Self {

src/pixel.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,24 @@ impl Rgb {
777777
Self { r, g, b }
778778
}
779779

780-
/// Parses an RGB pixel from a hex value.
780+
/// Parses an RGB pixel from a hex string.
781781
///
782782
/// The hex value can be in one of the following formats:
783783
/// - RRGGBB
784784
/// - RGB
785785
///
786-
/// These can be optionally padded with #, for example "#FF0000" is the same as as "FF0000".
786+
/// These can be optionally padded with #, for example "#FF0000" is the same as "FF0000".
787+
///
788+
/// # Note
789+
/// This is a relatively expensive operation. Use [`Rgb::from_u32`] if you know the hex value
790+
/// (i.e. if you are defining a constant), since you can write a `u32` using an integer literal
791+
/// of radix 16, e.g. `0xff0000`.
787792
///
788793
/// # Errors
789794
/// * Received a malformed hex code.
795+
///
796+
/// # See Also
797+
/// * [`Rgb::from_u32`] for parsing from an integer (better for known hex values).
790798
pub fn from_hex(hex: impl AsRef<str>) -> Result<Self> {
791799
let hex = hex.as_ref();
792800

@@ -822,6 +830,26 @@ impl Rgb {
822830
})
823831
}
824832

833+
/// Resolves the RGB pixel from a 32-bit integer.
834+
///
835+
/// Note that RGB pixels are inherently 24-bits, so the leading 8 bits of the integer are
836+
/// ignored (they are "padding bits").
837+
///
838+
/// # Examples
839+
/// ```
840+
/// # use ril::prelude::*;
841+
/// const RED: Rgb = Rgb::from_u32(0xff0000);
842+
/// assert_eq!(RED, Rgb::new(255, 0, 0));
843+
/// ```
844+
#[must_use]
845+
pub const fn from_u32(value: u32) -> Self {
846+
Self {
847+
r: (value >> 16) as u8,
848+
g: (value >> 8) as u8,
849+
b: value as u8,
850+
}
851+
}
852+
825853
/// Creates a completely black pixel.
826854
#[must_use]
827855
pub const fn black() -> Self {
@@ -1020,7 +1048,7 @@ impl Rgba {
10201048
Self::new(r, g, b, 255)
10211049
}
10221050

1023-
/// Parses an RGBA pixel from a hex value.
1051+
/// Parses an RGBA pixel from a hex string.
10241052
///
10251053
/// The hex value can be in one of the following formats:
10261054
/// - RRGGBBAA
@@ -1030,8 +1058,16 @@ impl Rgba {
10301058
///
10311059
/// These can be optionally padded with #, for example "#FF0000" is the same as as "FF0000".
10321060
///
1061+
/// # Note
1062+
/// This is a relatively expensive operation. Use [`Rgba::from_u32`] if you know the hex value
1063+
/// (i.e. if you are defining a constant), since you can write a `u32` using an integer literal
1064+
/// of radix 16, e.g. `0xff0000ff`.
1065+
///
10331066
/// # Errors
10341067
/// * Received a malformed hex code.
1068+
///
1069+
/// # See Also
1070+
/// * [`Rgba::from_u32`] for parsing from an integer (better for known hex values).
10351071
pub fn from_hex(hex: &str) -> Result<Self> {
10361072
let hex = hex.strip_prefix('#').unwrap_or(hex);
10371073

@@ -1064,6 +1100,27 @@ impl Rgba {
10641100
}
10651101
}
10661102

1103+
/// Resolves the RGBA pixel from a 32-bit integer (0xRRGGBBAA).
1104+
///
1105+
/// Consider using [`Rgb::from_u32`] and then [`TrueColor::into_rgba`] if you have a
1106+
/// conventional 24-bit RGB integer, as this method will not properly handle that.
1107+
///
1108+
/// # Examples
1109+
/// ```
1110+
/// # use ril::prelude::*;
1111+
/// const RED: Rgba = Rgba::from_u32(0xff0000ff);
1112+
/// assert_eq!(RED, Rgba::new(255, 0, 0, 255));
1113+
/// ```
1114+
#[must_use]
1115+
pub const fn from_u32(value: u32) -> Self {
1116+
Self {
1117+
r: (value >> 24) as u8,
1118+
g: (value >> 16) as u8,
1119+
b: (value >> 8) as u8,
1120+
a: value as u8,
1121+
}
1122+
}
1123+
10671124
/// Creates a completely transparent pixel.
10681125
#[must_use]
10691126
pub const fn transparent() -> Self {

src/sequence.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ impl<P: Pixel> ImageSequence<P> {
259259
///
260260
/// If you are limited by this trait bound, you can either specify the image format manually
261261
/// using [`from_read`][Self::from_read], or you can try using [`ImageFormat::infer_encoding`]
262-
/// along with [`from_read`][Self::from_read] manually instead. If you are able to use
263-
/// [`from_bytes`][Self::from_bytes] instead, which takes a byte slice instead of a `Read`
264-
/// stream, you can either that or [`from_bytes_inferred`][Self::from_bytes_inferred], too,
262+
/// along with [`from_read`][Self::from_read] manually instead. If you are able to use
263+
/// [`from_bytes`][Self::from_bytes] instead, which takes a byte slice instead of a `Read`
264+
/// stream, you can either that or [`from_bytes_inferred`][Self::from_bytes_inferred], too,
265265
/// which does not require a `Write` bound either.
266266
///
267267
/// # Errors
268-
/// * [`DecodingError`][crate::Error::DecodingError]: The image could not be decoded, maybe it
268+
/// * [`DecodingError`][crate::Error::DecodingError]: The image could not be decoded, maybe it
269269
/// is corrupt.
270-
/// * [`UnknownEncodingFormat`][crate::Error::UnknownEncodingFormat]: Could not infer the
270+
/// * [`UnknownEncodingFormat`][crate::Error::UnknownEncodingFormat]: Could not infer the
271271
/// encoding from the image. Try explicitly specifying it.
272272
///
273273
/// # Panics
@@ -323,7 +323,7 @@ impl<P: Pixel> ImageSequence<P> {
323323
/// Decodes an image sequence from the given byte slice, inferring its encoding.
324324
/// Could be useful in conjunction with the `include_bytes!` macro.
325325
///
326-
/// This is more efficient than [`from_read_inferred`][Self::from_read_inferred], and can act as
326+
/// This is more efficient than [`from_read_inferred`][Self::from_read_inferred], and can act as
327327
/// a workaround for bypassing the `Write` trait bound.
328328
///
329329
/// This decodes frames lazily as an iterator. Call [`DynamicFrameIterator::into_sequence`] to

0 commit comments

Comments
 (0)