pub enum Color {
Named(NamedColor),
Hex(String),
}Expand description
Text color representation (either named or hex)
Variants§
Implementations§
Source§impl Color
impl Color
Sourcepub fn to_named(&self) -> Option<NamedColor>
pub fn to_named(&self) -> Option<NamedColor>
Gets the named color for a given Color.
If the color is not a named color, it will try to find a matching named color for a hex color.
Examples found in repository?
src/minimessage.rs (line 530)
520 fn serialize_object(&mut self, obj: &ComponentObject) -> Result<(), MiniMessageError> {
521 // Save current style to compare changes
522 let prev_style = self.current_style.clone();
523
524 // Apply style changes
525 let mut style_changes = Vec::new();
526
527 if let Some(color) = &obj.color
528 && Some(color) != prev_style.color.as_ref()
529 {
530 if let Some(named) = color.to_named() {
531 style_changes.push(named.to_string());
532 } else if let Color::Hex(hex) = color {
533 style_changes.push(format!("color:{hex}"));
534 }
535 }
536
537 if obj.bold != prev_style.bold && obj.bold == Some(true) {
538 style_changes.push("bold".to_string());
539 }
540
541 if obj.italic != prev_style.italic && obj.italic == Some(true) {
542 style_changes.push("italic".to_string());
543 }
544
545 if obj.underlined != prev_style.underlined && obj.underlined == Some(true) {
546 style_changes.push("underlined".to_string());
547 }
548
549 if obj.strikethrough != prev_style.strikethrough && obj.strikethrough == Some(true) {
550 style_changes.push("strikethrough".to_string());
551 }
552
553 if obj.obfuscated != prev_style.obfuscated && obj.obfuscated == Some(true) {
554 style_changes.push("obfuscated".to_string());
555 }
556
557 // Apply style changes
558 for change in &style_changes {
559 self.output.push_str(&format!("<{change}>"));
560 }
561
562 // Update current style
563 self.current_style = Style {
564 color: obj.color.clone(),
565 bold: obj.bold,
566 italic: obj.italic,
567 underlined: obj.underlined,
568 strikethrough: obj.strikethrough,
569 obfuscated: obj.obfuscated,
570 ..self.current_style.clone()
571 };
572
573 // Serialize text content
574 if let Some(text) = &obj.text {
575 self.serialize_text(text)?;
576 }
577
578 // Serialize children
579 if let Some(extra) = &obj.extra {
580 for comp in extra {
581 self.serialize_component(comp)?;
582 }
583 }
584
585 // Close style changes
586 for change in style_changes.iter().rev() {
587 self.output.push_str(&format!("</{change}>"));
588 }
589
590 // Restore previous style
591 self.current_style = prev_style;
592
593 Ok(())
594 }Trait Implementations§
Source§impl<'de> Deserialize<'de> for Color
impl<'de> Deserialize<'de> for Color
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
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 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