Closed Bug 848745 Opened 7 years ago Closed 7 years ago

API to get CSSColor names from triplets and vice versa

Categories

(Core :: CSS Parsing and Computation, defect)

defect
Not set

Tracking

()

RESOLVED FIXED
mozilla22

People

(Reporter: miker, Assigned: bzbarsky)

References

Details

Attachments

(2 files, 2 obsolete files)

It would be useful if you could expose the list of all color names along with their values to privileged JS. This is useful for e.g. converting rgb values to color names.

There are numerous lists of all color names and values in .js files so it would be great if we had a way to get this information directly from the browser.

Something like:
{
  "red", { r: 255, g: 0, b: 0 },
  "green", { r:0, g:255, b:0 },
  ...
}

We access the color name list in TestColorNames.cpp so it should be simple. I am just not sure how this should be exposed to JS.
I can certainly add an API on the inspector utils that returns an object like the one you describe if that's what you'd prefer.

Alternately we can do some sort of APIs that take a name and return a triple and vice versa...

What would work best for you?  It seems like the "convert rgb value to color name" use case is actually a bit of a PITA given the data structure in comment 0...
(In reply to Boris Zbarsky (:bz) from comment #1)
> I can certainly add an API on the inspector utils that returns an object
> like the one you describe if that's what you'd prefer.
> 
> Alternately we can do some sort of APIs that take a name and return a triple
> and vice versa...
> 

Color name to triple and vice versa sounds like it is what we would need.
OK.  So take a color name and return an { r: R, g: G, b: B }?  And for the triple-to-name take a triple as three arguments or a dictionary or something else?
I would say something like:
xxx.getCSSTripleToName(255, 0 ,0) returns "red"
xxx.getCSSNameTOTriple("red") returns {r: 255, g: 0, b: 0}
OK.  I should be able to get this together on Monday.
Assignee: nobody → bzbarsky
Michael, I made this throw for unknown color names; let me know if you'd prefer a different behavior
Attachment #724768 - Flags: review?(mratcliffe)
Attachment #724768 - Flags: review?(dbaron)
Again, I made this throw for RGB triples that don't correspond to a color name
Attachment #724776 - Flags: review?(mratcliffe)
Attachment #724776 - Flags: review?(dbaron)
Comment on attachment 724768 [details] [diff] [review]
part 1.  Add scriptable APIs for converting CSS color names to RGB triples.

Could you add a comment noting that using |octet| for rgb() color values isn't necessarily future-proof since their range can exceed 0-255, but it's ok for this usage in particular since it's only about current named colors?  I just don't want this pattern to be copied somewhere it's inappropriate.

r=dbaron with that
Attachment #724768 - Flags: review?(dbaron) → review+
Attachment #724768 - Flags: review?(mratcliffe)
Attachment #724768 - Flags: review?(dbaron)
Attachment #724768 - Flags: review+
Comment on attachment 724776 [details] [diff] [review]
part 2.  Add a scriptable API for converting RGB triples to CSS color names.

It seems better to export another method from nsColor.cpp/nsColor.h and use the array there rather than storing the list of color names twice.
Attachment #724776 - Flags: review?(dbaron) → review-
Attachment #724776 - Flags: review?(mratcliffe)
Attachment #724776 - Flags: review?(dbaron)
Attachment #724776 - Flags: review-
Attachment #724776 - Flags: review+
Comment on attachment 724768 [details] [diff] [review]
part 1.  Add scriptable APIs for converting CSS color names to RGB triples.

resetting review flag (See comment 8)
Attachment #724768 - Flags: review?(dbaron) → review+
Comment on attachment 724776 [details] [diff] [review]
part 2.  Add a scriptable API for converting RGB triples to CSS color names.

resetting review flag (see comment 9)
Attachment #724776 - Flags: review?(dbaron) → review-
Attachment #724776 - Attachment is obsolete: true
> Could you add a comment noting

Done.
Comment on attachment 725013 [details] [diff] [review]
part 2.  Add a scriptable API for converting RGB triples to CSS color names.

Review of attachment 725013 [details] [diff] [review]:
-----------------------------------------------------------------

::: gfx/src/nsColor.cpp
@@ +291,5 @@
> +
> +NS_GFX_(const char*)
> +NS_RGBToColorName(nscolor aColor)
> +{
> +  for(size_t idx = 0; idx < ArrayLength(kColors); ++idx) {

'for ('
Attachment #725023 - Flags: review?(dbaron)
Attachment #725013 - Attachment is obsolete: true
Attachment #725013 - Flags: review?(dbaron)
Comment on attachment 725023 [details] [diff] [review]
Part 2 with the space

Probably add a comment that in case of multiple matches, it returns the first one in nsColorNameList.h, which is generally the first in alphabetical order?  (Assuming that's the case.)

From a US English perspective, I'm happy with that, since gray is before grey in alphabetical order. :-)
Attachment #725023 - Flags: review?(dbaron) → review+
Summary: API to get all CSSColor names and values → API to get CSSColor names from triplets and vice versa
You need to log in before you can comment on or make changes to this bug.