# Handling color in Plots user recipe that produces an image, or indexed color

**URL:** https://discourse.julialang.org/t/handling-color-in-plots-user-recipe-that-produces-an-image-or-indexed-color/59769
**Category:** Visualization
**Tags:** plots, recipe
**Created:** [April 22, 2021, 1:18am UTC](https://discourse.julialang.org/t/handling-color-in-plots-user-recipe-that-produces-an-image-or-indexed-color/59769 "2021-04-22T01:18:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)
#### Post date: [April 22, 2021, 1:18am UTC](https://discourse.julialang.org/t/handling-color-in-plots-user-recipe-that-produces-an-image-or-indexed-color/59769/1 "2021-04-22T01:18:28Z")

</div>

I have a `Grid` type, and I have a way to plot it in terms of an image (`Array{RBGA{...}, 2}`), so I define the following user recipe:

```julia
using Plots
using Plots: RGBA
using RecipesBase
using OffsetArrays

struct Grid
    x::OffsetArray{Bool, 2, Array{Bool, 2}}
end

@recipe function f(s::Grid)
    # TODO use :seriescolor and :seriesalpha in plotattributes
    full = RGBA(0.0, 0.0, 1.0, 0.5)
    empty = RGBA(0.0, 0.0, 0.0, 0.0)
    axs = axes(s.x)
    f = first.(axs) .- 0.5
    l = last.(axs) .+ 0.5
    _range(start, step, stop) = start:step:stop
    extents = map(_range, f, [1.0, 1.0], l)
    (d1, d2) = extents
    d1, d2, ifelse.(transpose(s.x.parent), full, empty)
end

a = fill(false, (-5:5, 0:10))
a[:] .= rand(Bool, 11, 11)[:]
plot(Grid(a))

```

which produces

![image](https://global.discourse-cdn.com/julialang/original/3X/c/9/c99edfa9d2d9fa268f66eab31a08d1a233f7945a.png)

What’s a good way to handle e.g. `plot(Grid(a), color=:red, alpha=0.2)` (which would correspond to `full = RGBA(1.0, 0.0, 1.0, 0.2)`) ? I imagine roughly two strategies:

1. handle the conversion to `RGBA` in my recipe
2. find some plot type that would handle that conversion for me

---

<div class="post-metadata">

### Author: ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)
#### Post date: [April 22, 2021, 10:11pm UTC](https://discourse.julialang.org/t/handling-color-in-plots-user-recipe-that-produces-an-image-or-indexed-color/59769/2 "2021-04-22T22:11:18Z")

</div>

The conversion I spoke of is done by:

> <https://github.com/JuliaPlots/Plots.jl/blob/ddf6aa976012cf5b091c3d46a9796536c255883e/src/args.jl#L1607-L1616>

and e.g.

> <https://github.com/JuliaPlots/PlotUtils.jl/blob/4007370cd3c3b9a6cdc62871687dac21364190ff/src/colors.jl#L6-L12>

It’s probably against the spirit of a user recipe to do this conversion in the recipe.

---

<div class="post-metadata">

### Author: ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)
#### Post date: [April 22, 2021, 11:46pm UTC](https://discourse.julialang.org/t/handling-color-in-plots-user-recipe-that-produces-an-image-or-indexed-color/59769/3 "2021-04-22T23:46:41Z")

</div>

I’ve opened a feature request: [[FR] indexed image with `:auto` color · Issue #3448 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/3448)
