# Override or change opacity of a given RGBA color?

**URL:** <https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880>\
**Category:** Visualization\
**Tags:** question, colors\
**Created:** [November 29, 2023, 7:04am UTC](https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880 "2023-11-29T07:04:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [November 29, 2023, 7:04am UTC](https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880/1 "2023-11-29T07:04:25Z")

</div>

I’ve seen a construct like `(:red, 0.3)` to control the color and opacity separately. What I’d like to achieve here is to change the opacity of a given RGBA color. What’s the idiomatic way?

At the end of this message, I show a self-contained, simplified example (which currently uses a fake function that pretends to do what I want to do).

Probably, to people who are familiar with dealing with user-defined types (`struct`s), this problem should be very simple. Currently, I don’t know how to (safely) copy a user-defined-type object. (Perhaps I should use `copy()`?) I also don’t know how to find out the names of the fields. (What’s the name of the alpha channel? Julia includes powerful reflection, so this should be easy.) I also don’t know whether direct assignment to a field is recommended/allowed or not. (In some languages, you use setter functions, instead of allowing for direct assignments.) In some languages, there are idioms like `newobj = setfield(oldobj, fieldname = val)` . . .

[In that sense, it may have been better to ask these questions in a different forum, but I just wondered whether there is an idiomatic color-specific way.]

```julia
using CairoMakie

"""
Fake: ignores the input color and returns a fixed color.
"""
function set_opacity(c, alpha)
return Makie.RGBA{Float32}(0.9019608f0,0.62352943f0,0.0f0,alpha)
end

xax = -π:(π/16):π
var1 = cos.(xax)
var2 = sin.(xax)
cs = Makie.wong_colors() # Vector of RGBAs
fig = Figure()
ax = Axis(fig[1,1])
lines!(ax, xax, var1; color=cs[2])
lines!(ax, xax, var2; color=set_opacity(cs[2], 0.3f0), linewidth=10)
fig

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 29, 2023, 10:10am UTC](https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880/2 "2023-11-29T10:10:31Z")

</div>

In Makie you can also do `(rgba_color, somealpha)` although that will multiply. Otherwise there’s `Colors.alphacolor` I think

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [November 29, 2023, 1:47pm UTC](https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880/3 "2023-11-29T13:47:39Z")

</div>

> [@jules](#):
>
> `(rgba_color, somealpha)`

Works! Thanks!

> [@jules](#):
>
> although that will multiply.

I see. That means that you need to know the alpha value of the original color. For my plots at hand, that is perfectly fine because all alpha values are 1 in `wong_colors()`.

> [@jules](#):
>
> Colors.alphacolor

So, I guess what you do would be

```julia
newcol = copy(oldcol)
newcol.alphacolor = 0.3

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 29, 2023, 2:10pm UTC](https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880/4 "2023-11-29T14:10:01Z")

</div>

I meant the `alphacolor` function from Colors.jl

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [December 1, 2023, 5:05pm UTC](https://discourse.julialang.org/t/override-or-change-opacity-of-a-given-rgba-color/106880/5 "2023-12-01T17:05:24Z")

</div>

> [@jules](#):
>
> I meant the `alphacolor` function from [Colors.jl](https://juliahub.com/ui/Packages/Colors)

I see! I’ve just tried it. I think that’s the best way in that it doesn’t depend on `Makie` (and so it should work with other graphics packages, too):

```julia
julia> using Colors

julia> a = RGBA(0.5, 0.6, 0.7, 0.8)
RGBA{Float64}(0.5,0.6,0.7,0.8)

julia> coloralpha(a, 0.1) # replace alpha
RGBA{Float64}(0.5,0.6,0.7,0.1)

```
