JuliaAttic / Color.jl

Basic color manipulation utilities.
Other
47 stars 21 forks source link

Add RGBA to enable transparency in plots #16

Closed timholy closed 10 years ago

timholy commented 10 years ago

I wanted to specify a filled region with a partially-transparent color; RGBA(color("blue"), alpha) seems to be a reasonable approach.

JeffBezanson commented 10 years ago

I'm all in favor. Maybe one could argue that alpha channels aren't a "color" issue, but there isn't really any other reasonable place to put this.

dcjones commented 10 years ago

Including alpha make practical sense, even though its a rendering option and not a color. I'm kind of uncomfortable with it defined this way though.

Currently there are 10 types derived from ColorValue and they can all be converted between mostly without loss. There's no way to convert RGBA to any other type without dropping the alpha channel, so making it derive ColorValue makes it a weird exception. Eg. you can't convert it to LAB, tweak it, and convert it back to RGBA without losing the alpha channel.

I'd like it if alpha was added in a more general way. Something like:

immutable AlphaColorValue{T <: ColorValue}
    c::T
    alpha::Float64

    function AlphaColorValue(x1, x2, x3, alpha=1.0)
        new(T(x1, x2, x3), alpha)
    end
end

typealias RGBA AlphaColorValue{RGB}
# Etc for the other Colorvalue types

function convert{T,U}(::Type{AlphaColorValue{T}, c::AlphaColorValue{U}}
    AlphaColorValue{T}(convert(T, c.c), c.alpha)
end
timholy commented 10 years ago

Good thinking; see if you like this better.

The only thing I wrestled with was whether RGBA32 should go straight to Uint32, but I decided to retain symmetry with the other types.

JeffBezanson commented 10 years ago

That is much better. Too bad RGBA32 has to be so different from RGB24.

timholy commented 10 years ago

(Since Jeff looked at it, I realized I had forgotten to export AlphaColorValue. That has been fixed.)

dcjones commented 10 years ago

Looks good!

timholy commented 10 years ago

A fun bit of perspective: exporting figures from Matlab always works best if you can use a vector format, but I first noticed, oh, a decade ago, that transparency forces you to use bitmap. At intervals I sent them email asking them to add good direct-to-PDF support. I'm still waiting. The release notes for 2013b indicates they've finally added this for OSX, but I think other platforms are still out of luck.

And here, it took a fraction of an afternoon.