Skip to main content

Color

Struct Color 

Source
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}
Expand description

RGBA color value.

Fields§

§r: u8

Red component (0-255).

§g: u8

Green component (0-255).

§b: u8

Blue component (0-255).

§a: u8

Alpha component (0-255, 0=transparent, 255=opaque).

Implementations§

Source§

impl Color

Source

pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self

Create a new color.

Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Create an opaque color.

Source

pub const fn white() -> Self

White color.

Source

pub const fn black() -> Self

Black color.

Examples found in repository?
examples/render_subtitle.rs (line 65)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    // Example SRT subtitle data
9    let srt_data = r"1
1000:00:01,000 --> 00:00:04,000
11Hello, World!
12
132
1400:00:05,000 --> 00:00:08,000
15This is a subtitle example
16with multiple lines.
17
183
1900:00:10,000 --> 00:00:13,000
20<i>Styled subtitle with HTML tags</i>
21";
22
23    // Parse SRT subtitles
24    let subtitles = srt::parse(srt_data.as_bytes())?;
25    println!("Parsed {} subtitles", subtitles.len());
26
27    for (i, subtitle) in subtitles.iter().enumerate() {
28        println!(
29            "Subtitle {}: {}ms - {}ms: {}",
30            i + 1,
31            subtitle.start_time,
32            subtitle.end_time,
33            subtitle.text
34        );
35    }
36
37    // Create a sample video frame
38    let mut frame = VideoFrame::new(PixelFormat::Rgb24, 1920, 1080);
39    frame.allocate();
40
41    // Fill with blue background
42    if let Some(plane) = frame.planes.get_mut(0) {
43        let data_len = plane.data.len();
44        let mut new_data = vec![0u8; data_len];
45        for pixel in new_data.chunks_exact_mut(3) {
46            pixel[0] = 0; // R
47            pixel[1] = 0; // G
48            pixel[2] = 100; // B
49        }
50        plane.data = new_data;
51    }
52
53    // Note: In a real application, you would load a font file
54    // For this example, we'll show how to create the renderer
55    println!("\nTo use the subtitle renderer:");
56    println!("1. Load a font: Font::from_file(\"path/to/font.ttf\")?");
57    println!("2. Configure style with colors, size, outline, etc.");
58    println!("3. Create renderer: SubtitleRenderer::new(font, style)");
59    println!("4. Render: renderer.render_subtitle(&subtitle, &mut frame, timestamp)?");
60
61    // Example style configuration
62    let style = SubtitleStyle::default()
63        .with_font_size(48.0)
64        .with_color(255, 255, 255, 255) // White text
65        .with_outline(OutlineStyle::new(Color::black(), 2.0))
66        .with_position(Position::bottom_center())
67        .with_alignment(Alignment::Center)
68        .with_margins(40, 40, 40, 80);
69
70    println!(
71        "\nStyle configuration: Font size={}, Color=white, Outline=black 2px",
72        style.font_size
73    );
74
75    // Example timestamps
76    let ts1 = Timestamp::new(2000, Rational::new(1, 1000)); // 2 seconds
77    let ts2 = Timestamp::new(6000, Rational::new(1, 1000)); // 6 seconds
78
79    // Show which subtitles would be active at different times
80    for subtitle in &subtitles {
81        #[allow(clippy::cast_possible_truncation)]
82        let ts1_ms = (ts1.to_seconds() * 1000.0).round() as i64;
83        #[allow(clippy::cast_possible_truncation)]
84        let ts2_ms = (ts2.to_seconds() * 1000.0).round() as i64;
85        if subtitle.is_active(ts1_ms) {
86            println!(
87                "\nAt t=2s, active subtitle: '{}'",
88                subtitle.text.lines().next().unwrap_or("")
89            );
90        }
91        if subtitle.is_active(ts2_ms) {
92            println!(
93                "At t=6s, active subtitle: '{}'",
94                subtitle.text.lines().next().unwrap_or("")
95            );
96        }
97    }
98
99    // Example: Write subtitles back to SRT format
100    let output_srt = srt::write(&subtitles)?;
101    println!("\nGenerated SRT output:\n{output_srt}");
102
103    Ok(())
104}
Source

pub const fn transparent() -> Self

Transparent color.

Source

pub fn from_hex(hex: &str) -> Result<Self, SubtitleError>

Parse from hex string (e.g., “#FFFFFF” or “#FFFFFFFF”).

§Errors

Returns error if the string is not a valid hex color.

Source

pub fn blend_over(&self, background: Color) -> Color

Blend this color with another using alpha compositing.

Source

pub const fn with_alpha(&self, a: u8) -> Self

Create a color with modified alpha.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Color

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Color

Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnsafeUnpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.