CartoDB / carto-vl

CARTO VL: a Javascript library to create vector-based visualizations
BSD 3-Clause "New" or "Revised" License
129 stars 26 forks source link

Add option to getLegendData to return colors in a different format than RGBA #1278

Open elenatorro opened 5 years ago

elenatorro commented 5 years ago

Context

When using getLegendData method, for instance, in a Ramp in order to assign colors to values, you get the color in RGBA format. We've discussed in the past that this is the best way to return colors. However, in almost all of the examples where we're using this method, we have the following function to convert the color from RGBA to HEX because it's easier for us when using it in the HTML

function rgbToHex(color) {
    return "#" + ((1 << 24) + (color.r << 16) + (color.g << 8) + color.b).toString(16).slice(1);
}

Since getLegendData can receive options, such as the number the label for the 'others' values, it'd be great to have another one that let you change the color format.

What do you think?

VictorVelarde commented 5 years ago

👍 Nice idea!

If we're already going to provide options to that method, then I would for it. I found that piece of code in the examples quite obscure for the end user. We could add something like:

{
   colorFormat: 'hex' /* keeping 'rgba' as default, to keep compatibility */
}
VictorVelarde commented 5 years ago

Right now we have some examples using rgba without that ugly function and I think that it can be enough, should we close this one @elenatorro ?

elenatorro commented 5 years ago

I'd prefer to keep it since it's not related only with the examples but in other use cases. I wouldn't close it, I'd just give it a low priority or just label it as an enhancement.