# Copying a color

**URL:** <https://discourse.processing.org/t/copying-a-color/12312>\
**Category:** Coding Questions\
**Created:** [June 26, 2019, 4:05pm UTC](https://discourse.processing.org/t/copying-a-color/12312 "2019-06-26T16:05:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcusround](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@marcusround](https://discourse.processing.org/u/marcusround)\
**Post date:** [June 26, 2019, 4:05pm UTC](https://discourse.processing.org/t/copying-a-color/12312/1 "2019-06-26T16:05:31Z")

</div>

Is this the best/most succinct way of copying a color?

```auto
c2 = color(
  red(c1),
  green(c1),
  blue(c1),
  alpha(c1)
)

```

I wish I could just do c2 = c1.copy() or something like that, which you can do with vectors.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 26, 2019, 4:21pm UTC](https://discourse.processing.org/t/copying-a-color/12312/2 "2019-06-26T16:21:16Z")

</div>

> [@marcusround](#):
>
> I wish I could just do `c2 = c1.copy()` or something like that,

We can clone a [[u]p5.Color[/u]](http://p5js.org/reference/#/p5.Color) object by passing it as an argument for [**color()**](http://p5js.org/reference/#/p5/color): `c2 = color(c1)` ©

However, given a [[u]p5.Color[/u]](http://p5js.org/reference/#/p5.Color) object can be reused everywhere, there’s no point cloning it. 🙄

---

<div class="post-metadata">

**Author:** ![marcusround](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@marcusround](https://discourse.processing.org/u/marcusround)\
**Post date:** [June 26, 2019, 4:40pm UTC](https://discourse.processing.org/t/copying-a-color/12312/3 "2019-06-26T16:40:36Z")

</div>

If I do `c2 = color(c1)` and then make changes to `c1`, then `c2` is also changed. I don’t see any need for you to roll your eyes at me.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 26, 2019, 5:13pm UTC](https://discourse.processing.org/t/copying-a-color/12312/4 "2019-06-26T17:13:59Z")

</div>

> [@marcusround](#):
>
> If I do `c2 = color(c1)` and then make changes to _c1_ , then _c2_ is also changed.

Oops! I thought that would clone it; but it seems like it merely returns the passed argument. 🥴

A p5.Object keeps its current RGBa component values in an array stored in its undocumented property _levels[]_. 🧐

And although somewhat hidden within the **color()** function’s reference: 🕶

> **[reference | p5.js](https://p5js.org/reference/#/p5/color)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

It has an overloaded signature which accepts an array argument: 😉

> `values` Number: an array containing the red,green,blue & and alpha components of the color.

So this now should properly clone the p5.Color object: `c2 = color(c1.levels)`. 😤

But still, I can’t see the point of making changes to any of the 4 properties of a p5.Color object. 🤔

---

<div class="post-metadata">

**Author:** ![marcusround](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@marcusround](https://discourse.processing.org/u/marcusround)\
**Post date:** [June 27, 2019, 9:08am UTC](https://discourse.processing.org/t/copying-a-color/12312/5 "2019-06-27T09:08:52Z")

</div>

Thanks for your help, that works perfectly.
