Fix failing default_color.60 test

Allow converting a brush to a color. In the case of a gradient, the color of the first stop is returned.

For the C++ generator this requires adding the extra case of explicitly
calling the `Brush(const Color &)` constructor, despite it being implicit,
in order to generate the correct code when we have IR that casts twice:

```
   Expression::Cast {
       from: Expression::Cast {
           from: Expression::Cast {
               from: Expression::NumberLiteral(...),
               to: Type::Color,
           }
           to: Type::Brush,
       },
       to: Type::Color,
   }
```
This commit is contained in:
Simon Hausmann 2021-02-04 13:44:10 +01:00
parent 4e4817e041
commit 50d9211e0a
6 changed files with 49 additions and 0 deletions

View file

@ -33,6 +33,8 @@ public:
}
// TODO: Add function to return span for stops?
const GradientStop *stopsBegin() const { return inner.begin() + 1; }
const GradientStop *stopsEnd() const { return inner.end(); }
private:
cbindgen_private::types::LinearGradientBrush inner;
@ -57,6 +59,8 @@ public:
Brush(const Color &color) : data(Inner::SolidColor(color.inner)) { }
Brush(const LinearGradientBrush &gradient) : data(Inner::LinearGradient(gradient.inner)) { }
inline Color color() const;
friend bool operator==(const Brush &a, const Brush &b) { return a.data == b.data; }
friend bool operator!=(const Brush &a, const Brush &b) { return a.data != b.data; }
@ -66,4 +70,23 @@ private:
Inner data;
};
Color Brush::color() const
{
Color result;
switch (data.tag) {
case Tag::NoBrush:
break;
case Tag::SolidColor: {
result.inner = data.solid_color._0;
break;
}
case Tag::LinearGradient:
if (data.linear_gradient._0.size() > 1) {
result.inner = data.linear_gradient._0[1].color;
}
break;
}
return result;
}
}