1mod eyes;
15mod motion;
16mod pixels;
17mod shape;
18
19pub use eyes::{Eye, Eyes};
20pub use motion::{Beat, Motion};
21pub use pixels::{GRID, mascot};
22pub use shape::{Lobe, SAMPLES, Shape, seed};
23
24use gpui::{
25 AnyElement, Bounds, Hsla, IntoElement, PathBuilder, Pixels, Point, Styled, Window, canvas,
26 hsla, point, px,
27};
28use theme::{Theme, contrast_ratio, flatten};
29
30const FILL: f32 = 0.4;
32
33#[derive(Clone, Copy, Debug, PartialEq)]
35pub struct Face {
36 pub shape: Shape,
37 pub eyes: Eyes,
38 pub motion: Motion,
39 pub color: Option<Hsla>,
41}
42
43impl Face {
44 pub fn new(shape: Shape) -> Self {
45 Self {
46 shape,
47 eyes: Eyes::default(),
48 motion: Motion::default(),
49 color: None,
50 }
51 }
52
53 pub fn eyes(mut self, eyes: Eyes) -> Self {
54 self.eyes = eyes;
55 self
56 }
57
58 pub fn motion(mut self, motion: Motion) -> Self {
59 self.motion = motion;
60 self
61 }
62
63 pub fn color(mut self, color: Hsla) -> Self {
64 self.color = Some(color);
65 self
66 }
67}
68
69impl From<u64> for Face {
70 fn from(seed: u64) -> Self {
71 Self::new(Shape::from(seed)).eyes(Eyes::from(seed))
72 }
73}
74
75impl From<&str> for Face {
76 fn from(name: &str) -> Self {
77 Self::from(seed(name))
78 }
79}
80
81impl Face {
82 pub fn pose(&self, t: f32) -> Pose {
85 let beat = self.motion.beat(t);
86 let shape = self.motion.shape(self.shape, t);
87 let breath = |(x, y): (f32, f32)| (x * beat.scale, y * beat.scale);
88 Pose {
89 outline: shape.outline().map(breath),
90 eyes: self.eyes.place(&shape, self.motion.drift).map(|e| {
91 let (cx, cy) = breath((e.cx + beat.gaze.0, e.cy + beat.gaze.1));
92 Eye {
93 cx,
94 cy,
95 rx: e.rx * beat.scale,
96 ry: e.ry * beat.scale * (1.0 - beat.lid * 0.92),
97 ..e
98 }
99 }),
100 color: self.color,
101 }
102 }
103}
104
105#[derive(Clone, Copy, Debug, PartialEq)]
109pub struct Pose {
110 pub outline: [(f32, f32); SAMPLES],
111 pub eyes: [Eye; 2],
112 pub color: Option<Hsla>,
113}
114
115impl Pose {
116 pub fn lerp(a: &Self, b: &Self, k: f32) -> Self {
119 let k = k.clamp(0.0, 1.0);
120 let f = |x: f32, y: f32| x + (y - x) * k;
121 Self {
122 outline: std::array::from_fn(|i| {
123 (
124 f(a.outline[i].0, b.outline[i].0),
125 f(a.outline[i].1, b.outline[i].1),
126 )
127 }),
128 eyes: std::array::from_fn(|i| Eye {
129 cx: f(a.eyes[i].cx, b.eyes[i].cx),
130 cy: f(a.eyes[i].cy, b.eyes[i].cy),
131 rx: f(a.eyes[i].rx, b.eyes[i].rx),
132 ry: f(a.eyes[i].ry, b.eyes[i].ry),
133 rot: f(a.eyes[i].rot, b.eyes[i].rot),
134 n: f(a.eyes[i].n, b.eyes[i].n),
135 }),
136 color: match (a.color, b.color) {
139 (Some(x), Some(y)) => Some(flatten(hsla(y.h, y.s, y.l, k), x)),
140 (x, y) => {
141 if k < 0.5 {
142 x
143 } else {
144 y
145 }
146 }
147 },
148 }
149 }
150}
151
152pub fn avatar(pose: Pose) -> AnyElement {
154 canvas(
155 move |_: Bounds<Pixels>, _, _| (),
156 move |bounds, (), window, cx| {
157 let theme = Theme::of(cx);
158 let head = pose.color.unwrap_or(theme.accent);
159 let text = flatten(theme.text, head);
161 let ink = if contrast_ratio(head, theme.bg) >= contrast_ratio(head, text) {
162 theme.bg
163 } else {
164 theme.text
165 };
166 paint(window, bounds, &pose, head, ink);
167 },
168 )
169 .size_full()
170 .into_any_element()
171}
172
173fn paint(window: &mut Window, bounds: Bounds<Pixels>, pose: &Pose, head: Hsla, ink: Hsla) {
174 let span = bounds.size.width.min(bounds.size.height).to_f64() as f32;
175 let unit = span * FILL;
176 let mid = bounds.center();
177 let map = |x: f32, y: f32| point(mid.x + px(x * unit), mid.y + px(y * unit));
178
179 window.paint_layer(bounds, |window| {
180 fill(
181 window,
182 map(pose.outline[0].0, pose.outline[0].1),
183 spline(&pose.outline, &map),
184 head,
185 );
186 for eye in &pose.eyes {
187 let (start, segs) = superellipse(eye, &map);
188 fill(window, start, segs, ink);
189 }
190 });
191}
192
193type Cubic = (Point<Pixels>, Point<Pixels>, Point<Pixels>);
194
195fn fill(window: &mut Window, start: Point<Pixels>, segs: Vec<Cubic>, color: Hsla) {
196 let mut b = PathBuilder::fill();
197 b.move_to(start);
198 for (end, c1, c2) in segs {
199 b.cubic_bezier_to(end, c1, c2);
200 }
201 b.close();
202 if let Ok(path) = b.build() {
203 window.paint_path(path, color);
204 }
205}
206
207fn spline(pts: &[(f32, f32); SAMPLES], map: &impl Fn(f32, f32) -> Point<Pixels>) -> Vec<Cubic> {
210 let at = |i: isize| pts[i.rem_euclid(SAMPLES as isize) as usize];
211 (0..SAMPLES as isize)
212 .map(|i| {
213 let ((x0, y0), (x1, y1), (x2, y2), (x3, y3)) = (at(i - 1), at(i), at(i + 1), at(i + 2));
214 (
215 map(x2, y2),
216 map(x1 + (x2 - x0) / 6.0, y1 + (y2 - y0) / 6.0),
217 map(x2 - (x3 - x1) / 6.0, y2 - (y3 - y1) / 6.0),
218 )
219 })
220 .collect()
221}
222
223fn superellipse(e: &Eye, map: &impl Fn(f32, f32) -> Point<Pixels>) -> (Point<Pixels>, Vec<Cubic>) {
226 let k = ((8.0 * 2f32.powf(-1.0 / e.n) - 4.0) / 3.0).min(1.0);
229 let (ak, bk) = (e.rx * k, e.ry * k);
230 let (rx, ry) = (e.rx, e.ry);
231 let pts = [
232 (rx, 0.0),
233 (rx, bk),
234 (ak, ry),
235 (0.0, ry),
236 (-ak, ry),
237 (-rx, bk),
238 (-rx, 0.0),
239 (-rx, -bk),
240 (-ak, -ry),
241 (0.0, -ry),
242 (ak, -ry),
243 (rx, -bk),
244 (rx, 0.0),
245 ];
246 let (sin, cos) = e.rot.to_radians().sin_cos();
247 let at = |(x, y): (f32, f32)| map(e.cx + x * cos - y * sin, e.cy + x * sin + y * cos);
248 (
249 at(pts[0]),
250 (1..13)
251 .step_by(3)
252 .map(|i| (at(pts[i + 2]), at(pts[i]), at(pts[i + 1])))
253 .collect(),
254 )
255}