问题
Can anyone help me find out how can I can get the color of rectangle in corona? That rectangle I already filled with color, so now I want to get that color when I touch on rectangle.
回答1:
Create your rectangle:
local rectangle = display.newRect(0, 0, 100, 100)
Put your color in RGBA (you can leave out the A) format in a table, and store it as a "custom property" for the rectangle:
rectangle.fillColor = {110, 30, 25}
Through the magic of the unpack function, which returns the values of a table, pass the table to setFillColor:
rectangle:setFillColor( unpack(rectangle.fillColor) )
Now you can always get the color like so:
print( unpack(rectangle.fillColor) ) --> 110 30 25
or
print( rectangle.fillColor ) -- simply returns the table
or to put each color in a variable:
local red, green, blue, alpha = unpack(rectangle.fillColor)
You'll see how this can come in handy for other things as well.
EDIT
Just thought of another cool way of doing it, by highjacking the setFillColor function:
local function decorateRectangle(rect)
rect.cachedSetFillColor = rect.setFillColor -- store setFillColor function
function rect:setFillColor(...) -- replace it with our own function
self:cachedSetFillColor(...)
self.storedColor = {...} -- store color
end
function rect:getFillColor()
return unpack(self.storedColor)
end
end
local rectangle = display.newRect(0, 0, 100, 100)
decorateRectangle(rectangle) -- "decorates" rectangle with more awesomeness
Now you can use setFillColor to set color as normal, AND getFillColor to return it :)
rectangle:setFillColor(100, 30, 255, 255)
print(rectangle:getFillColor())
回答2:
in short: you cannot 'get' the fill colour. you have to save it yourself..
回答3:
This is also another method to obtain color of the rect.
create a table of colors
local colors = { {255,0,0}, {0,0,255}, {0,255,0}, {255, 255, 0}, {255,0,255}}
Then when you create your rectangle, make it look like this:
local rect = display.newRect(0, 0,100,100) rect.color = math.random(1,5) rect:setFillColor(unpack(colors[rect.color]))
Now if you want to obtain the color of the rect do like this
local appliedcolor = colors[rect.color];
Thanks to https://forums.coronalabs.com/topic/18414-get-fill-color-of-an-object/
回答4:
A DisplayObject's many property values, including RGB values for the fill color, can be inspected using object._properties. DisplayObject introspection was added in Build 2014.2511, i.e. two years after this question was asked.
The information is returned as a JSON formatted string, so the json.*
library is used to extract the property (properties) of interest.
Assuming a DisplayObject called object
, you would do something like:
local json = require( "json" )
local decoded, pos, msg = json.decode( object._properties )
if not decoded then
-- handle the error (which you're not likely to get)
else
local fill = decoded.fill
print( fill.r, fill.g, fill.b, fill.a )
end
来源:https://stackoverflow.com/questions/11239054/how-can-i-get-the-colour-fill-of-a-displayobject-in-response-to-an-event-like