lux/private/
canvas.rs

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
use super::primitive_canvas::{PrimitiveCanvas, StencilState, StencilType};
use super::accessors::DrawParamMod;
use super::types::Float;
use super::gfx_integration::{ColorVertex, TexVertex};
use super::color::Color;
use super::raw::{Colored, Transform};
use super::sprite::Sprite;

use glium::index::PrimitiveType::{TriangleFan, TrianglesList, Points};

use vecmath;

const OPT_LINE_LENGTH: u16 = 15;

fn calc_delta_theta(segments: Option<u16>,
                    line_length: Option<u16>,
                    width: Float,
                    height: Float) -> Float
{
    use std::f32::consts::PI;
    let largest_radius = width.max(height);
    match (segments, line_length.unwrap_or(OPT_LINE_LENGTH)) {
        (Some(segment_count), _) => {
            (2.0 * PI) / (segment_count as Float)
        }
        (None, line_length) => {
            // = (2 pi) / ((2pi r) / (line_len))
            // = line_len / r
            line_length as Float / largest_radius
        }
    }
}

struct BasicFields<'a, C: 'a> {
    fill_color: [Float; 4],
    stroke_color: Option<[Float; 4]>,
    border: Float,
    transform: [[Float; 4]; 4],

    pos: (Float, Float),
    size: (Float, Float),
    canvas: &'a mut C
}

/// An ellipse that can be drawn to the screen.
#[must_use = "Ellipses only contain context, and must be drawn with `fill()`"]
pub struct Ellipse<'a, C: 'a> {
    fields: BasicFields<'a, C>,
    segments: Option<u16>,
    opt_line_len: Option<u16>
}

/// A Rectangle that can be drawn to the screen.
#[must_use = "Rectangles only contain context, and must be drawn with `fill()`, `stroke()`, or `fill_stroke()`"]
pub struct Rectangle<'a, C: 'a> {
    fields: BasicFields<'a, C>,
}

/// A sprite that can be drawn to the screen.
#[must_use = "Sprites only contain context, and must be drawn with `draw()`"]
pub struct ContainedSprite<'a, C: 'a>  {
    fields: BasicFields<'a, C>,
    sprite: Sprite
}

/// Canvas is the main trait for drawing in Lux.  It supports all operations
/// that paint to the screen or to a buffer.
pub trait Canvas: PrimitiveCanvas + Colored + Transform + DrawParamMod+ Sized {
    /// Returns the size of the canvas as a pair of (width, height).
    fn size(&self) -> (Float, Float);

    /// Returns the size of the canvas in integer form.
    fn size_i(&self) -> (i32, i32) {
        let (w, h) = self.size();
        (w as i32, h as i32)
    }

    /// Returns the width of the canvas.
    fn width(&self) -> Float {
        match self.size() {
            (w, _) => w
        }
    }

    /// Returns the height of the canvas.
    fn height(&self) -> Float {
        match self.size() {
            (_, h) => h
        }
    }

    /// Returns the width of the canvas in integer form.
    fn width_i(&self) -> i32 {
        self.width() as i32
    }

    /// Returns the height of the canvas in integer form.
    fn height_i(&self) -> i32 {
        self.width() as i32
    }

    /// Clears the canvas with a solid color.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// // Clear the screen with purple.
    /// frame.clear(rgb(1.0, 0.0, 1.0));
    ///# }
    /// ```
    fn clear<C: Color>(&mut self, color: C) {
        PrimitiveCanvas::clear(self, color);
    }

    /// Evaluates the function with a canvas that will only draw into the
    /// provided rectangle.
    fn with_scissor<F, R>(&mut self, x: u32, y: u32, w: u32, h: u32, f: F) -> R
    where F: FnOnce(&mut Self) -> R {
        // Flush things that we don't want scissored.
        self.flush_draw().unwrap();

        let view_height = self.height() as u32;
        let old = self.take_scissor();
        // TODO: merge these rectangles
        self.set_scissor(Some((x, view_height - h - y, w, h)));
        let res = f(self);
        self.flush_draw().unwrap();
        self.set_scissor(old);
        res
    }

    /// Executes a drawing function where all drawing is done on the
    /// stencil buffer.
    fn draw_to_stencil<R, S>(&mut self, typ: StencilType, stencil_fn: S) -> R
    where S: FnOnce(&mut Self) -> R {
        self.flush_draw().unwrap();
        self.set_stencil_state(StencilState::DrawingStencil(typ));

        let res1 = stencil_fn(self);
        self.flush_draw().unwrap();
        self.set_stencil_state(StencilState::DrawingWithStencil);
        res1
    }

    /// Clears the stencil buffer allowing all draws to go though.
    ///
    /// When called with `StencilType::Allow`, the stencil buffer will
    /// be cleared allowing all future draws to pass through until
    /// `draw_to_stencil` is called with `StencilType::Deny`.
    ///
    /// Whene called with `StencilType::Deny`, the stencil buffer will be
    /// filled, preventing all future draws to fail until `draw_to_stencil`
    /// is called with `StencilType::Allow`.
    fn clear_stencil(&mut self, typ: StencilType) {
        self.flush_draw().unwrap();
        match typ {
            StencilType::Allow => {
                PrimitiveCanvas::clear_stencil(self, 1);
                self.set_stencil_state(StencilState::None);
            }
            StencilType::Deny => {
                PrimitiveCanvas::clear_stencil(self, 0);
                self.set_stencil_state(StencilState::DrawingWithStencil);
            }
        }
    }

    /// Returns a rectangle with the given dimensions and position.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let (x, y, w, h) = (0.0, 5.0, 50.0, 70.0);
    /// frame.rect(x, y, w, h)
    ///      .color(rgb(100, 200, 255))
    ///      .fill();
    ///# }
    /// ```
    fn rect<'a>(&'a mut self, x: Float, y: Float, w: Float, h: Float) -> Rectangle<'a, Self> {
        let c = self.get_color();
        Rectangle::new(self, (x, y), (w, h), c)
    }

    /// Returns a square with the given dimensions and position.
    ///
    /// ```rust,no_run
    /// use lux::prelude::*;
    ///# extern crate lux;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let (x, y, size) = (0.0, 5.0, 50.0);
    /// frame.square(x, y, size)
    ///      .color(rgb(100, 200, 255))
    ///      .fill();
    ///# }
    /// ```
    fn square<'a>(&'a mut self, x: Float, y: Float, size: Float) -> Rectangle<'a, Self> {
        let c = self.get_color();
        Rectangle::new(self, (x, y), (size, size), c)
    }

    /// Returns an ellipse with the given dimensions and position.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let (x, y, w, h) = (0.0, 5.0, 50.0, 70.0);
    /// frame.ellipse(x, y, w, h)
    ///      .color(rgb(100, 200, 255))
    ///      .fill();
    ///# }
    /// ```
    fn ellipse<'a>(&'a mut self, x: Float, y: Float, w: Float, h: Float) -> Ellipse<'a, Self> {
        let c = self.get_color();
        Ellipse::new(self, (x, y), (w, h), c)
    }

    /// Returns an circle with the given dimensions and position.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let (x, y, size) = (0.0, 5.0, 50.0);
    /// frame.circle(x, y, size)
    ///      .color(rgb(100, 200, 255))
    ///      .fill();
    ///# }
    /// ```
    fn circle<'a>(&'a mut self, x: Float, y: Float, size: Float) -> Ellipse<'a, Self> {
        let c = self.get_color();
        Ellipse::new(self, (x, y), (size, size), c)
    }

    /// Draws a 1-pixel colored point to the screen at a position.
    ///
    /// This is *not* the same as setting a "pixel" because the point can
    /// be moved by transformations on the Frame.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let (x, y) = (10.0, 20.0);
    /// frame.draw_point(x, y, lux::color::RED);
    ///# }
    /// ```
    fn draw_point<C: Color>(&mut self, x: Float, y: Float, color: C) {
        let vertex = ColorVertex {
            pos: [x, y],
            color: color.to_rgba(),
        };
        self.draw_colored(Points, &[vertex][..], None, None).unwrap();
    }

    /// Draws a sequence of colored points with the size of 1 pixel.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    /// use lux::graphics::ColorVertex;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let points = [
    ///     ColorVertex {
    ///         pos: [10.0, 15.0],
    ///         color: rgb(255, 0, 0)
    ///     },
    ///     ColorVertex {
    ///         pos: [15.0, 10.0],
    ///         color: rgb(0, 255, 0)
    ///     },
    /// ];
    /// frame.draw_points(&points[..]);
    ///# }
    /// ```
    fn draw_points(&mut self, pixels: &[ColorVertex]) {
        let mut transf = vecmath::mat4_id();
        transf.translate(0.5, 0.5); // Correctly align
        self.draw_colored(Points, &pixels[..], None, Some(transf)).unwrap();
    }

    /// Draws a single line from `start` to `end` with a
    /// thickness of `line_size`.
    fn draw_line(&mut self, x1: Float, y1: Float, x2: Float, y2: Float, line_size: Float) {
        let dx = x2 - x1;
        let dy = y2 - y1;
        let dist = (dx * dx + dy * dy).sqrt();
        let angle = dy.atan2(dx);

        self.rect(0.0, 0.0, dist, line_size)
            .translate(x1, y1)
            .rotate(angle)
            .translate(0.0, -line_size / 2.0)
            .fill();
    }

    /// Draws a series of lines from each point to the next with a thickness
    /// of `line_size`.
    fn draw_lines<I: Iterator<Item = (Float, Float)>>(&mut self, mut positions: I, line_size: Float) {
        let mut prev = match positions.next() {
            Some(p) => p,
            None => return
        };

        for p in positions {
            self.draw_line(prev.0, prev.1, p.0, p.1, line_size);
            prev = p;
        }
    }

    /// Draws an arc centered at `pos` from `angle1` to `angle_2` with a
    /// thickness of `line_size`.
    fn draw_arc(&mut self, pos: (Float, Float), radius: Float, angle1: Float,
                angle2: Float, line_size: Float) {

        use std::f32::consts::PI;

        const PI_2: f32 = PI * 2.0;

        fn norm(mut value: f32) -> Float {
            value %= PI_2;
            value = (value + PI_2) % PI_2;
            value
        }

        fn gen_point(offset: (Float, Float), radius: Float, angle: Float) -> (Float, Float) {
            (angle.sin() * radius + offset.0, angle.cos() * radius + offset.1)
        }

        let angle1 = norm(angle1);
        let angle2 = norm(angle2);
        let delta = norm(angle2 - angle1);

        // TODO: When you switch this over to being state based, switch
        // these None valuse
        let delta_theta = calc_delta_theta(None, None, radius, radius);

        let mut points = vec![];

        let mut theta = 0.0;
        while theta <= delta {
            points.push(gen_point(pos, radius, theta));
            theta += delta_theta;
        }
        points.push(gen_point(pos, radius, theta));

        self.with_rotate_around(pos, -angle1, |c| {
            c.draw_lines(points.into_iter(), line_size);
        });
    }

    /// Draws a sprite  to the screen.
    ///
    /// ```rust,no_run
    ///# extern crate lux;
    /// use std::path::Path;
    /// use lux::prelude::*;
    /// use lux::graphics::ColorVertex;
    ///# fn main() {
    ///
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let logo = window.load_texture_file(&Path::new("./logo.png"))
    ///                  .unwrap()
    ///                  .into_sprite();
    /// let (x, y) = (20.0, 50.0);
    /// frame.sprite(&logo, x, y).draw();
    ///# }
    /// ```
    fn sprite(&mut self, sprite: &Sprite, x: Float, y: Float) -> ContainedSprite<Self> {
        ContainedSprite {
            fields: BasicFields::new((x, y), sprite.ideal_size(), self, [1.0, 1.0, 1.0, 1.0]),
            sprite: sprite.clone()
        }
    }
}

impl <'a, C: 'a> BasicFields<'a, C> {
    fn new(pos: (Float, Float), size: (Float, Float), c: &'a mut C, color: [Float; 4]) -> BasicFields<'a, C> {
        BasicFields {
            fill_color: color,
            stroke_color: None,
            border: 0.0,
            transform: vecmath::mat4_id(),

            pos: pos,
            size: size,
            canvas: c
        }
    }
}

impl <'a, C> Ellipse<'a, C> {
    fn new(c: &'a mut C, pos: (Float, Float), size: (Float, Float), color: [Float; 4]) -> Ellipse<'a, C> {
        Ellipse {
            fields: BasicFields::new(pos, size, c, color),
            segments: None,
            opt_line_len: None
        }
    }

    /// Sets the number of segments that are used to approximate a circle.
    ///
    /// ### Example
    /// ```rust,no_run
    ///# extern crate lux;
    /// use lux::prelude::*;
    /// use lux::graphics::ColorVertex;
    ///
    ///# fn main() {
    /// let mut window = Window::new().unwrap();
    /// let mut frame = window.frame();
    /// let (x, y, size) = (10.0, 10.0, 50.0);
    /// // draw a pentagon
    /// frame.circle(x, y, size).segments(5).draw();
    ///# }
    /// ```
    pub fn segments(&mut self, segments: u16) -> &mut Self {
        self.segments = Some(segments);
        self
    }

    /// Instead of
    pub fn line_length(&mut self, line_length: u16) -> &mut Self {
        self.opt_line_len = Some(line_length);
        self
    }
}

impl <'a, C> Rectangle<'a, C> {
    fn new(c: &'a mut C, pos: (Float, Float), size: (Float, Float), color: [Float; 4]) -> Rectangle<'a, C> {
        Rectangle {
            fields: BasicFields::new(pos, size, c, color),
        }
    }
}

impl <'a, C> Transform for Rectangle<'a, C> {
    fn current_matrix(&self) -> &[[Float; 4]; 4] {
        &self.fields.transform
    }
    fn current_matrix_mut(&mut self) -> &mut[[Float; 4]; 4] {
        &mut self.fields.transform
    }
}

impl <'a, C> Transform for Ellipse<'a, C> {
    fn current_matrix(&self) -> &[[Float; 4]; 4] {
        &self.fields.transform
    }
    fn current_matrix_mut(&mut self) -> &mut[[Float; 4]; 4] {
        &mut self.fields.transform
    }
}

impl <'a, C> Transform for ContainedSprite<'a, C> {
    fn current_matrix(&self) -> &[[Float; 4]; 4] {
        &self.fields.transform
    }
    fn current_matrix_mut(&mut self) -> &mut[[Float; 4]; 4] {
        &mut self.fields.transform
    }
}

impl <'a, C> Colored for Ellipse<'a, C> {
    fn get_color(&self) -> [Float; 4] {
        self.fields.fill_color
    }

    fn color<A: Color>(&mut self, color: A) -> &mut Self {
        self.fields.fill_color = color.to_rgba();
        self
    }
}

impl <'a, C> Colored for Rectangle<'a, C> {
    fn get_color(&self) -> [Float; 4] {
        self.fields.fill_color
    }

    fn color<A: Color>(&mut self, color: A) -> &mut Self{
        self.fields.fill_color = color.to_rgba();
        self
    }
}

impl <'a, C> Colored for ContainedSprite<'a, C> {
    fn get_color(&self) -> [Float; 4] {
        self.fields.fill_color
    }

    fn color<A: Color>(&mut self, color: A) -> &mut Self{
        self.fields.fill_color = color.to_rgba();
        self
    }
}

impl <'a, C> Ellipse<'a, C> where C: Canvas + 'a {
    /// Fills in the ellipse with a solid color.
    pub fn fill(&mut self) {
        use std::f32::consts::PI;
        use num::traits::Float as Nfloat;

        let delta_theta = calc_delta_theta(self.segments,
                                           self.opt_line_len,
                                           self.fields.size.0,
                                           self.fields.size.1);
        let color = self.get_color();
        let mut vertices = vec![];

        let mut theta = 0.0;
        while theta <= 2.0 * PI {
            let p = [theta.sin(), theta.cos()];
            vertices.push(ColorVertex { pos: p, color: color });
            theta += delta_theta;
        }

        //let mut trx = vecmath::mat4_id();
        //trx.scale(0.5, 0.5);

        let mut transform = generate_transform(&self.fields);

        //trx = vecmath::col_mat4_mul(trx, transform);
        transform.translate(0.5, 0.5);
        transform.scale(0.5, 0.5);

        self.fields.canvas.draw_colored(TriangleFan,
                               &vertices[..],
                               None,
                               Some(transform)).unwrap()
    }
}

fn generate_transform<'a, C>(fields: &BasicFields<'a, C>) -> [[Float; 4]; 4] {
        let (x, y) = fields.pos;
        let (mut sx, mut sy) = fields.size;
        sx -= fields.border * 2.0;
        sy -= fields.border * 2.0;

        if sx < 0.0 { sx = 0.0 }
        if sy < 0.0 { sy = 0.0 }

        let mut trx = vecmath::mat4_id();
        trx.translate(x, y);
        let mut trx = vecmath::col_mat4_mul(trx, fields.transform);
        trx.translate(fields.border, fields.border);
        trx.scale(sx, sy);
        trx
}

impl <'a, C> ContainedSprite<'a, C> where C: Canvas + 'a {
    /// Sets the side of the sprite when drawn to the screen.
    ///
    /// The default size is the "ideal size", that is, 1 pixel in the texture
    /// goes to 1 pixel on the screen.
    pub fn size(&mut self, w: Float, h: Float) -> &mut ContainedSprite<'a, C> {
        self.fields.size = (w, h);
        self
    }

    /// Draws the sprite to the screen.
    pub fn draw(&mut self) {
        let bounds = self.sprite.bounds();

        let top_left = bounds[0];
        let top_right = bounds[1];
        let bottom_left = bounds[2];
        let bottom_right = bounds[3];

        let tex_vs = vec![
            TexVertex {pos: [1.0, 0.0], tex_coords: top_right},
            TexVertex {pos: [0.0, 0.0], tex_coords: top_left},
            TexVertex {pos: [0.0, 1.0], tex_coords: bottom_left},
            TexVertex {pos: [1.0, 1.0], tex_coords: bottom_right},
        ];

        let idxs = [0, 1, 2, 0, 2, 3];

        let transform = generate_transform(&self.fields);

        self.fields.canvas.draw_tex(TrianglesList,
                      &tex_vs[..],
                      Some(&idxs[..]),
                      Some(transform),
                      self.sprite.texture(),
                      Some(self.fields.fill_color)).unwrap();
    }
}

impl <'a, C> Rectangle<'a, C> where C: Canvas + 'a {
    /// Fills the rectangle with a solid color.
    pub fn fill(&mut self) {
        let color = self.get_color();
        let vertices = [
            ColorVertex{ pos: [1.0, 0.0], color: color },
            ColorVertex{ pos: [0.0, 0.0], color: color },
            ColorVertex{ pos: [0.0, 1.0], color: color },
            ColorVertex{ pos: [1.0, 1.0], color: color },
        ];

        let idxs = [0, 1, 2, 0, 2, 3];

        let transform = generate_transform(&self.fields);

        self.fields.canvas.draw_colored(TrianglesList,
                               &vertices[..], Some(&idxs[..]),
                               Some(transform)).unwrap();
    }

    /// Draws a border around the rectangle.
    pub fn stroke(&mut self) {
        let offset_pos = self.fields.pos;
        let size = self.fields.size;
        let border = self.fields.border;
        let transform = self.fields.transform;
        let color = self.fields.stroke_color.unwrap_or(self.get_color());

        self.fields.border = 0.0;

        self.fields.canvas.with_matrix(|canvas| {
            canvas.translate(offset_pos.0, offset_pos.1);
            canvas.apply_matrix(transform);
            canvas.with_color(color, |canvas| {
                // TOP
                canvas.rect(0.0, 0.0, size.0, border).fill();
                canvas.rect(0.0, size.1 - border, size.0, border).fill();
                canvas.rect(0.0, border, border, size.1 - border * 2.0).fill();
                canvas.rect(size.0 - border, border, border, size.1 - border * 2.0).fill();
            });
        });
    }

    /// Both fills and strokes the rectangle.
    pub fn fill_and_stroke(&mut self) {
        self.fill();
        self.stroke();
    }

    /// Sets the size of the border.  The border is drawn using the
    /// `stroke()` function.
    pub fn border<A: Color>(&mut self, border_size: Float, color: A) -> &mut Rectangle<'a, C> {
        self.fields.border = border_size;
        self.fields.stroke_color = Some(color.to_rgba());
        self
    }
}