Copying a color

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

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.

1 Like

We can clone a [u]p5.Color[/u] object by passing it as an argument for color(): c2 = color(c1) :copyright:

However, given a [u]p5.Color[/u] object can be reused everywhere, there’s no point cloning it. :roll_eyes:

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.

1 Like

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

A p5.Object keeps its current RGBa component values in an array stored in its undocumented property levels[]. :face_with_monocle:

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

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

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). :triumph:

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

3 Likes

Thanks for your help, that works perfectly.