# RGB conversion to and from an Array, broadcasting

**URL:** https://discourse.julialang.org/t/rgb-conversion-to-and-from-an-array-broadcasting/64721
**Category:** New to Julia
**Created:** [July 15, 2021, 11:59pm UTC](https://discourse.julialang.org/t/rgb-conversion-to-and-from-an-array-broadcasting/64721 "2021-07-15T23:59:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)
#### Post date: [July 15, 2021, 11:59pm UTC](https://discourse.julialang.org/t/rgb-conversion-to-and-from-an-array-broadcasting/64721/1 "2021-07-15T23:59:43Z")

</div>

Hi,  
I’ve just had a couple of days with Julia - I’m very much enjoying the Pluto, Jupyter and repl environments. Python has been making me bilious.

I have some very ‘early-learner’ questions related to idioms for unpacking and repacking _single_ `RGB` values. The MIT [Introduction to Computational Thinking](https://computationalthinking.mit.edu/Spring21/) course has a very early exercise to write a function that inverts an `RGB` value. I can do it the blah blah way, but…

A. I know how to make an `RGB `value like this `RGB(-1.0, 0.0, 0.0)`, but I cannot find out how to convert an Array containing the same values to a single `RGB` value.

```nohighlight
v = (-1.0, 0.0, 0.0)
v2 = RGB(v) # I can find no examples

```

B. How do I unpack an `RGB` value into an array, without using `[c.r, c.g. c.b]`?  
I’d like to do something like `(r,g,b) = c`, or `c.asArray` - preserving order. _My brackets are in dissaray 🙂_

C. I’m disappointed because I can broadcast an operation over an array, but not over an `RBG` value:

```julia
c = RGB(-1.0, 0.0, 0.0)
v = (c.r, c.g, c.b)
v2 = 1 .- v # Works nicely
# I don't know how to covert back to RGB

```

I would like to be able to do:

```julia
c = RGB(-1.0, 0.0, 0.0)
c2 = 1 -. c

```

I tried looking at `cmap` but there were no examples simple enough for me to interpret at my level.

Cheers…

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [July 16, 2021, 12:15am UTC](https://discourse.julialang.org/t/rgb-conversion-to-and-from-an-array-broadcasting/64721/2 "2021-07-16T00:15:33Z")

</div>

To unpack a tuple or array into an RGB value, you can use the [`splat` operator](https://docs.julialang.org/en/v1/manual/faq/#...-splits-one-argument-into-many-different-arguments-in-function-calls) like this:

```julia
julia> v2 = RGB(v...)
RGB{Float64}(-1.0,0.0,0.0)

```

I think you want [`mapc`](https://github.com/JuliaGraphics/ColorTypes.jl#functions), not `cmap`. For your example,

```julia
julia> mapc(v -> 1 - v, c)
RGB{Float64}(2.0,1.0,1.0)

```

---

<div class="post-metadata">

### Author: ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)
#### Post date: [July 16, 2021, 2:06am UTC](https://discourse.julialang.org/t/rgb-conversion-to-and-from-an-array-broadcasting/64721/3 "2021-07-16T02:06:21Z")

</div>

Thanks very much! And there it is in the FAQ. 🤪  
An insight as to why broadcast doesn’t work on RGB instances?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 16, 2021, 3:27am UTC](https://discourse.julialang.org/t/rgb-conversion-to-and-from-an-array-broadcasting/64721/4 "2021-07-16T03:27:55Z")

</div>

Each RGB instance is supposed to be treated as if it were a scalar value. This makes it easier to broadcast over each RGB value collectively rather than each individual component.

See [GitHub - JuliaGraphics/ColorVectorSpace.jl: Treat colors as if they are n-vectors for the purposes of arithmetic](https://github.com/JuliaGraphics/ColorVectorSpace.jl) if you want to treat it like a vector.
