I have got an array of pixels(having x and y values). how to set color of pixel points to red
-
14-11-2019 - |
Frage
I just want to know how to set a particular pixel's colour to red? suppose x =37 y=54 and i want to change this pixel's colour to red. I have no clue how to do it. I have got the values of points around a particular object into an array of pixels using marching square algo.
Lösung
You cannot change the pixels of an existing CGImage
. You have to create a new CGImage
with the pixel changed. These are the steps:
- Create a
CGBitmapContext
withCGBitmapContextCreate
. - Draw the existing
CGImage
into it usingCGContextDrawImage
. - Draw the pixel using
CGContextSetFillColorWithColor
andCGContextFillRect
. - Create a new
CGImage
usingCGBitmapContextCreateImage
.
Instead of using CGContextSetFillColorWithColor
and CGContextFillRect
, you could tweak the bitmap data directly after retrieving a pointer to it with CGBitmapContextGetData
. That would be faster if you're going to do it a lot.
Also, if you're going to do it a lot, you will want to create the bitmap context and draw the original image into it just once, and keep the bitmap context around for diddling. But creating the new CGImage
from the bitmap context may be a bottleneck.
Andere Tipps
Your question is quite vague, but here's a general answer:
Pixel colours are usually represented with 3 or 4 bytes:
Red - Green - Blue - ( Alpha )
There should be a function available in the SDK you are using that enables you set these values for a pixel. You would set red to 255 and the others to 0 if you want a pure red colour.
If you are working with CCSprite, just change the color using the color
property of the sprite:
mySprite.color=ccc3(123,234,12); //use whatever color values for red, green, blue you want
max values for red, green, blue in ccc3
are 255; when maxed out, the color is the natural color of the sprite; you cannot go brighter, but changing these values will move to other colors or darken the image if all are changed by same amount down.
for pure red, use ccc3(255,0,0)