Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Color to represent colors #48

Merged
merged 5 commits into from
Jun 19, 2019
Merged

Use Color to represent colors #48

merged 5 commits into from
Jun 19, 2019

Conversation

raphlinus
Copy link
Contributor

Fixes #40

@raphlinus
Copy link
Contributor Author

A discussion question: should I make solid brush creation non-failable while I'm breaking the API, or is that a separate change? (I'd also want to make text format creation significantly less result-oriented)

@RazrFalcon
Copy link

should I make solid brush creation non-failable

I think you should.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple thoughts inline, a nice improvement though!

}

/// Opaque white.
pub const fn white() -> Color {
Copy link
Member

@cmyr cmyr Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glad that these are const fns. I might go a bit further, though, and add them as associated constants on the struct itself; Color::WHITE, Color::BLACK, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, that's been stable since 1.20, it's hard to keep track :)

@@ -87,13 +89,15 @@ pub trait RenderContext {
/// responsiblity? We could have a cache that is flushed when the Direct2D
/// render target is rebuilt. Solid brushes are super lightweight, but
/// other potentially retained objects will be heavier.
fn solid_brush(&mut self, rgba: u32) -> Result<Self::Brush, Error>;
fn solid_brush(&mut self, color: Color) -> Self::Brush;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we removed this API, and just had Impl Into<Brush> for Color? I could imagine needing to expose something for gradients (although possibly also not) but I'm not sure that this API is pulling its weight. Is brush creation expensive? Even in that case, we're rarely reusing brushes between draw calls or anything...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a branch where I'm trying to do this, but it turns out to be not anywhere nearly so easy; the SolidColorBrush::create method requires a RenderTarget (and there's some funny stuff about the brush being bound to this render target, but I think I can ease that by using a DeviceContext everywhere - I tested and this does work on Windows 7 SP1).

I did try making an IntoBrush associated trait that would take either an &Brush or a Color, but the type tetris got a bit much for me (the result should be a Cow to handle both cases, and needs to encode the lifetime).

Brush creation in D2D is a COM call that does an allocation, so it's not free. It does feel clunky though; a completely reasonable alternative is for the Rust type to be cheap and then have a cache to D2D brush objects. This goes a bit beyond my original intent of being a thin wrapper over D2D, but maybe in this case the simplification for users is worth it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sounds like future work.

.travis.yml Outdated
# See https://github.com/gtk-rs/cairo/issues/263
env:
- PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a rebase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should have done a rebase instead of a merge, but then it needs a force-push. In any case, it should turn out the same. Ah, git.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@raphlinus raphlinus merged commit 98a6eae into master Jun 19, 2019
@raphlinus raphlinus deleted the color_enum branch June 19, 2019 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A simple color type
3 participants