[Ruby] Color (revisited)
Posted: Mon Mar 12, 2018 9:51 am
It is such a small thing that I don't think it belongs to "user examples".
Two years or so ago, I talked about some hidden features of some classes. The Color class provides .getARGB and .setARGB. With those you can manipulate an existing Color instance instead of creating a new class instance. Doing so will unburden the GC, especially in situations, where a constant high redraw rate is involved.
But it was not until yesterday, that I discovered the support for the splat operator! While getARGB returns an array, setARGB expects single integer values (up to 4). With the splat operator, you can provide an array directly. For example
Also, there's ._dump. It seems to be used internally for marshalling, so I wouldn't recommend using it. However, it returns a string
Ok, that's it
Two years or so ago, I talked about some hidden features of some classes. The Color class provides .getARGB and .setARGB. With those you can manipulate an existing Color instance instead of creating a new class instance. Doing so will unburden the GC, especially in situations, where a constant high redraw rate is involved.
But it was not until yesterday, that I discovered the support for the splat operator! While getARGB returns an array, setARGB expects single integer values (up to 4). With the splat operator, you can provide an array directly. For example
- Code: Select all
a = Color.new(221)
b = Color.new(32, 64, 128)
a.setARGB(*b.getARGB)
Also, there's ._dump. It seems to be used internally for marshalling, so I wouldn't recommend using it. However, it returns a string
- Code: Select all
# continued code section
a._dump(nil) # returns "255:32:64:128"
Ok, that's it