pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}Expand description
RGBA color value.
Fields§
§r: u8Red component (0-255).
g: u8Green component (0-255).
b: u8Blue component (0-255).
a: u8Alpha component (0-255, 0=transparent, 255=opaque).
Implementations§
Source§impl Color
impl Color
Sourcepub const fn black() -> Self
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}Sourcepub const fn transparent() -> Self
pub const fn transparent() -> Self
Transparent color.
Sourcepub fn from_hex(hex: &str) -> Result<Self, SubtitleError>
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.
Sourcepub fn blend_over(&self, background: Color) -> Color
pub fn blend_over(&self, background: Color) -> Color
Blend this color with another using alpha compositing.
Sourcepub const fn with_alpha(&self, a: u8) -> Self
pub const fn with_alpha(&self, a: u8) -> Self
Create a color with modified alpha.
Trait Implementations§
impl Copy for Color
impl Eq for Color
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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