1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

use makepad_math::Vec4;
use crate::colorhex;

pub trait Vec4Ext{
    fn from_hex_str(hex: &str) -> Result<Vec4, ()> {Self::from_hex_bytes(hex.as_bytes())}
    fn from_hex_bytes(bytes: &[u8]) -> Result<Vec4, ()>;
    fn append_hex_to_string(&self, out:&mut String);
    fn color(value: &str) -> Vec4;
}

impl Vec4Ext for Vec4{
    fn append_hex_to_string(&self, out:&mut String) {
        fn int_to_hex(d: u8) -> char {
            if d >= 10 {
                return (d + 55) as char;
            }
            (d + 48) as char
        }
        
        let r = (self.x * 255.0) as u8;
        let g = (self.y * 255.0) as u8;
        let b = (self.z * 255.0) as u8;
        out.push(int_to_hex((r >> 4) & 0xf));
        out.push(int_to_hex((r) & 0xf));
        out.push(int_to_hex((g >> 4) & 0xf));
        out.push(int_to_hex((g) & 0xf));
        out.push(int_to_hex((b >> 4) & 0xf));
        out.push(int_to_hex((b) & 0xf));
    }
    
    fn color(value: &str) -> Vec4 {
        if let Ok(val) = Self::from_hex_str(value) {
            val
        }
        else {
            Vec4 {x: 1.0, y: 0.0, z: 1.0, w: 1.0}
        }
    }
    
    fn from_hex_bytes(bytes: &[u8]) -> Result<Vec4, ()> {
        let color = if bytes.len()>2 && bytes[0] == b'#' {
            colorhex::hex_bytes_to_u32(&bytes[1..])?
        }
        else {
            colorhex::hex_bytes_to_u32(bytes)?
        };
        Ok(Vec4 {
            x: (((color >> 24)&0xff) as f32) / 255.0,
            y: (((color >> 16)&0xff) as f32) / 255.0,
            z: (((color >> 8)&0xff) as f32) / 255.0,
            w: ((color&0xff) as f32) / 255.0,
        })
    }
}