ombre: Support floating point colors

4dee80ef759eb690f9e72b00d08b5da70e11fd4e
Alexis Sellier committed ago 1 parent 9928a710
ombre/src/lisp/evaluator.rs +24 -13
7 7
use std::path::PathBuf;
8 8
use std::rc::Rc;
9 9
use std::{collections::HashMap, iter::Peekable};
10 10
use std::{fmt, fs, iter, result};
11 11
12 -
use crate::gfx::Rgba8;
12 +
use crate::gfx::Rgba;
13 13
use crate::lisp::dynamic::{Dynamic, IsDynamic};
14 14
use crate::lisp::list::List;
15 15
use crate::math::{Vector2D, Vector3D, Vector4D};
16 16
17 17
use super::{Parser, Scanner, FILE_EXTENSION};
135 135
    Str(String),
136 136
    /// Eg. 42
137 137
    Integer(i64),
138 138
    /// Eg. `z`
139 139
    Char(char),
140 -
    /// Eg. #44ff88
141 -
    Color(Rgba8),
142 140
}
143 141
144 142
impl TryFrom<Expr> for Key {
145 143
    type Error = Expr;
146 144
149 147
            Expr::Bool(k) => Key::Bool(k),
150 148
            Expr::Atom(k) => Key::Atom(k),
151 149
            Expr::Str(k) => Key::Str(k),
152 150
            Expr::Integer(k) => Key::Integer(k),
153 151
            Expr::Char(k) => Key::Char(k),
154 -
            Expr::Color(k) => Key::Color(k),
155 152
            _ => return Err(e),
156 153
        };
157 154
        Ok(key)
158 155
    }
159 156
}
236 233
    /// Eg. 42
237 234
    Integer(i64),
238 235
    /// Eg. `z`
239 236
    Char(char),
240 237
    /// Eg. #44ff88
241 -
    Color(Rgba8),
238 +
    Color(Rgba<f32>),
242 239
    /// Eg. (fn [x y] (+ x y))
243 240
    Function(Function),
244 241
    /// Mutable reference.
245 242
    Ref(Rc<RefCell<Expr>>),
246 243
    /// Dynamic type. Can be any Rust type.
625 622
            Expr::Atom(s) => write!(f, ":{s}"),
626 623
            Expr::Str(s) => write!(f, "\"{}\"", s.escape_debug()),
627 624
            Expr::Float(n) => write!(f, "{n}"),
628 625
            Expr::Integer(n) => write!(f, "{n}"),
629 626
            Expr::Char(c) => write!(f, "`{c}`"),
630 -
            Expr::Color(c) => write!(f, "#{:02x}{:02x}{:02x}{:02x}", c.r, c.g, c.b, c.a),
627 +
            Expr::Color(c) => write!(f, "(rgba {} {} {} {})", c.r, c.g, c.b, c.a),
631 628
            Expr::Function(e) => write!(f, "<fn {:p}>", e.as_ptr()),
632 -
            Expr::Ref(r) => write!(f, "<ref {}>", RefCell::borrow(r)),
629 +
            Expr::Ref(r) => write!(f, "(ref {})", RefCell::borrow(r)),
633 630
            Expr::Dynamic(d) => write!(f, "<dyn {:p}>", d.as_ptr()),
634 631
        }
635 632
    }
636 633
}
637 634
641 638
            Key::Bool(b) => write!(f, ":{b}"),
642 639
            Key::Atom(s) => write!(f, ":{s}"),
643 640
            Key::Str(s) => write!(f, "\"{}\"", s.escape_debug()),
644 641
            Key::Integer(n) => write!(f, "{n}"),
645 642
            Key::Char(c) => write!(f, "`{c}`"),
646 -
            Key::Color(c) => write!(f, "#{:02x}{:02x}{:02x}{:02x}", c.r, c.g, c.b, c.a),
647 643
        }
648 644
    }
649 645
}
650 646
651 647
/// Built-in or user-defined function.
1108 1104
        env.intrinsic(
1109 1105
            "vec4",
1110 1106
            "Create a 4D vector from x,y,z,w components",
1111 1107
            intrinsic::vec4,
1112 1108
        );
1109 +
        env.intrinsic(
1110 +
            "rgba",
1111 +
            "Create an RGBA color from components",
1112 +
            intrinsic::rgba,
1113 +
        );
1113 1114
        env
1114 1115
    }
1115 1116
}
1116 1117
1117 1118
/// Iterator over syntax tree which returns evaluated expressions.
1968 1969
    pub fn vec4(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
1969 1970
        let (x, y, z, w) = <(f64, f64, f64, f64)>::try_from_args(args)?;
1970 1971
        Ok(Expr::Vector4D(Vector4D::new(x, y, z, w)))
1971 1972
    }
1972 1973
1974 +
    /// Create an RGBA color from its components.
1975 +
    pub fn rgba(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> {
1976 +
        let (r, g, b, a) = <(f32, f32, f32, f32)>::try_from_args(args)?;
1977 +
        Ok(Expr::Color(Rgba::new(r, g, b, a)))
1978 +
    }
1979 +
1973 1980
    fn fold<T: TryFrom<Expr, Error = Expr> + Into<Expr>>(
1974 1981
        args: impl IntoIterator<Item = Expr>,
1975 1982
        init: T,
1976 1983
        op: impl Fn(T, T) -> T,
1977 1984
    ) -> Result<Expr> {
2203 2210
2204 2211
    #[test]
2205 2212
    fn test_color() {
2206 2213
        expect(
2207 2214
            &["#443311"],
2208 -
            Expr::Color(Rgba8::new(0x44, 0x33, 0x11, 0xff)),
2215 +
            Expr::Color(Rgba8::new(0x44, 0x33, 0x11, 0xff).into()),
2216 +
        );
2217 +
        expect(
2218 +
            &["(rgba 1.0 0.0 0.3 1.0)"],
2219 +
            Expr::Color(Rgba::new(1.0, 0.0, 0.3, 1.0)),
2209 2220
        );
2210 2221
        expect(
2211 2222
            &["{#111111 #ff00ff}"],
2212 2223
            Expr::list([
2213 -
                Expr::Color(Rgba8::new(0x11, 0x11, 0x11, 0xff)),
2214 -
                Expr::Color(Rgba8::new(0xff, 0x00, 0xff, 0xff)),
2224 +
                Expr::Color(Rgba8::new(0x11, 0x11, 0x11, 0xff).into()),
2225 +
                Expr::Color(Rgba8::new(0xff, 0x00, 0xff, 0xff).into()),
2215 2226
            ]),
2216 2227
        );
2217 2228
    }
2218 2229
2219 2230
    #[test]
2362 2373
        expect(
2363 2374
            &["(let [d (dict [1 :x 2 :y 3 :z])] (lookup 2 d))"],
2364 2375
            Expr::Atom("y".into()),
2365 2376
        );
2366 2377
        expect(
2367 -
            &["(dict [`x` 1 #ff0000 2 41 :a])"],
2378 +
            &["(dict [`x` 1 \"fnord\" 2 41 :a])"],
2368 2379
            Expr::Dict(HashMap::from_iter([
2369 2380
                (Key::Char('x'), Expr::Integer(1)),
2370 -
                (Key::Color(Rgba8::new(0xff, 0, 0, 0xff)), Expr::Integer(2)),
2381 +
                (Key::Str("fnord".to_owned()), Expr::Integer(2)),
2371 2382
                (Key::Integer(41), Expr::Atom("a".into())),
2372 2383
            ])),
2373 2384
        );
2374 2385
        expect(&["(dict)"], Expr::Dict(HashMap::new()));
2375 2386
        expect(&["(dict [])"], Expr::Dict(HashMap::new()));
ombre/src/lisp/parser.rs +1 -1
97 97
                self.tokens.next();
98 98
                Expr::Char(c)
99 99
            }
100 100
            Token::Color(c) => {
101 101
                self.tokens.next();
102 -
                Expr::Color(c)
102 +
                Expr::Color(c.into())
103 103
            }
104 104
            Token::Str(s) => {
105 105
                self.tokens.next();
106 106
                Expr::Str(s.into_owned())
107 107
            }