Enum Color

Source
pub enum Color {
    Named(NamedColor),
    Hex(String),
}
Expand description

Text color representation (either named or hex)

Variants§

§

Named(NamedColor)

Predefined Minecraft color name

§

Hex(String)

Hex color code in #RRGGBB format

Implementations§

Source§

impl Color

Source

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 Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

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

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

impl<'de> Deserialize<'de> for Color

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Color

Source§

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

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

impl From<[u8; 3]> for Color

Source§

fn from([r, g, b]: [u8; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<(u8, u8, u8)> for Color

Source§

fn from((r, g, b): (u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Color

Source§

type Err = ParseColorError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Color

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
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 · 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 Serialize for Color

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Color

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 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<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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,