MichaelFenwick / Color

A simple Dart package exposing a Color class which can be used to create, convert, and compare colors.
http://pub.dartlang.org/packages/color
MIT License
52 stars 18 forks source link

Add CSS 3 color table and constructor that creates a Color by name #7

Closed timklge closed 9 years ago

timklge commented 9 years ago

I added a simple css 3 color code lookup table and constructors to create a color instance by providing a css 3 color name (e. g. "black"). Though this is a rather minor improvement I personally like to have the color names right in my code.

MichaelFenwick commented 9 years ago

This is certainly a good feature, and something that should be added. Before I merge it in though, I had two concerns with it. First, I'm thinking that the .named constructor might be better suited to live in the RgbColor class rather than the HexColor class since Rgb is the house of all the logic for that color space, and Hex is simply a lightweight syntax converter. The second thought is that rather than using an UnmodifiableMapView, would using a const {'name': 'colordata'} be simpler? Do you know of any performance benefits of one versus the other? Let me know your thoughts on this.

timklge commented 9 years ago

Yes, implementing the css3 color code table as a compile time constant would definitely be better, but although the language spec mentions that Map literals can be used as constants, the dart analyzer issues a warning when I try to write it that way. Because final variables are evaluated lazily in dart, memory for the map is not allocated until the feature is actually used, so the overall difference between const and the final UnmodifiableMapView is very minor in this case, I think.

I put the named-Constructor into the HexColor class because the lookup table contains the string hex values and the HexColor class already got that constructor. I thought it made sense to use that.