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.
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.
We can clone a [u]p5.Color[/u] object by passing it as an argument for color(): c2 = color(c1)
However, given a [u]p5.Color[/u] object can be reused everywhere, there’s no point cloning it.
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.
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:
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.
Thanks for your help, that works perfectly.