SymbolixAU / colourvalues

R library for assigning colours to values
https://symbolixau.github.io/colourvalues/
46 stars 6 forks source link

alpha should be smart enough to handle values in [0, 1] automatically #37

Closed MichaelChirico closed 5 years ago

MichaelChirico commented 5 years ago

I think it's more common for people to think of alpha as a percentage, so I think it's natural to expect colourvalues(x, alpha = .5) to "just work".

From a design perspective, it's basically trivial to just detect fractions in alpha and multiply them by 255... simple truncation should be OK as I think the visual difference between alpha = 128 and alpha = 129 must be close to 0.

Things get interesting at the edge cases like alpha = c(0, 1, 1, 0)... I guess very few people will be using alpha = c(0, 1, 1, 0) on the 255 scale, so the logic to handle this requires some assumptions but I think they are pretty lightweight.

I'm happy to implement this functionality as a PR if you see fit.

SymbolixAU commented 5 years ago

I think it's a simple implementation (on branch alpha), and is only necessary where alpha is a single value, since

colour_values(1:5, alpha = seq(0, 0.8, length.out = 5))

'just works' too.

However, what should colour_values(x, 1.0001) return? Is this example a too nuanced edge-case?

SymbolixAU commented 5 years ago

@MichaelChirico If I were to implement this, should I change the accepted values of alpha from [0,255] to [1,256], so that it's more obvious that anything in [0,1] will be treated as a percentage?

MichaelChirico commented 5 years ago

@SymbolixAU I think no... only 1 is ambiguous since 0 is the same for both interpretations. And I think 0 should be 0, not 1...

Will come back to this again when I'm off vacation...

SymbolixAU commented 5 years ago

I've merged this into master in this commit, so now alpha is

optional. Single value in [0,255] applied to all colours, or a decimal in [0, 1) (to indicate a percentage, noting 1 is excluded), or a vector of numeric values the same length as x. The numeric vector will be scaled into the range [0,255]. If a matrix palette is supplied this argument is ignored.

I'm still not entirely 100% sure about this... and I don't know why...

MichaelChirico commented 5 years ago

Hopefully this is a case of the beauty of dynamically-typed languages... 😬

MichaelChirico commented 5 years ago

Thanks! Can't wait to give it a spin

On Mon, Jan 14, 2019 at 5:05 AM SymbolixAU notifications@github.com wrote:

Closed #37 https://github.com/SymbolixAU/colourvalues/issues/37 via 530a73a https://github.com/SymbolixAU/colourvalues/commit/530a73a245595bc7583e0707f3801ae127ebcb5d .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/SymbolixAU/colourvalues/issues/37#event-2070718098, or mute the thread https://github.com/notifications/unsubscribe-auth/AHQQdT7ZhKu2s7hKjFRFoQF7TronTmffks5vC5-LgaJpZM4ZACbg .