Is it possible to make colors invisible in the code? - iphone

For a game that I am currently making, I have a black background with many white dots, which are being used to represent stars. I have no need for the black color however, and I only wish for the stars to show. My question is this: Is it possible to completely hide the black color without using an image manipulation program, that is, using only code? Any help is appreciated.

This is the form:
// some rgba color representation
struct t_rgba {
uint8_t r, g, b, a;
};
bool SetToTransparentIfPixelIsBlack(t_rgba* const rgba) {
// is it black?
if (0 == rgba->r && 0 == rgba->g && 0 == rgba->b) {
// then set the alpha value to transparent:
rgba->a = 0;
}
}
although the image data you are using will be represented differently.

Related

How can i convert hex color code to color name or rgb value?

The thing is i have an hex color code which is pass to another screen and when i go to another screen i wanna convert that hex color code to color name or to rgb value is that possible? need some help here.
This is my hex color code value type #ff373334
the package color_models will help you convert a Hex color to CMYK, HSI, HSL, HSP, HSB, LAB, Oklab, RGB, and XYZ.
It's straightforward to use, and if you need a custom implementation, you can check the repository of the package to see how each model work to convert your Hex color to RGB.
Its simple
new Color(0xFF373334)
should work
or
Color(0xFF373334).red
Color(0xFF373334).green
Color(0xFF373334).blue
You can use it like this.
hexStringToColor(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return Color(int.parse(hexColor, radix: 16));
}
Now you can call this function anywhere like this.
Color color = hexStringToColor(hexString);

tracking.js works with sample image but not more complex data

I'm trying to use tracking.js for color detection. I'm testing the code locally, and can replicate the example results when using the sample image provided in the tracking.js live demo.
I can also add a new color and get the tracker to identify the fourth bulb in that sample:
tracking.ColorTracker.registerColor('#8CF0BE', function(r, g, b) {
if (r < 150 && g > 225 && b < 200) {
return true;
}
return false;
});
But when I swap out the default image on my local test page with the one below (which is from an unrelated but excellent SO post, nothing at all gets highlighted/boxed.
Image by Mecanismo, via Wikimedia Commons. Used under the CC BY-SA 3.0 license.
I'm specifically trying to get the switch to light up, with this color definition:
tracking.ColorTracker.registerColor('orangered', function(r, g, b) {
if (r > g && r > b) {
return true;
}
return false;
});
I used a digital color meter to determine that all the switches in the pixel have red values in the 150-200 range, green values in the 30-100 range, and blue values in the 10-100 range (and mostly not the high ends of those scales).
For what it's worth, I know the custom color definition itself isn't completely broken, because it does have an effect on that initial sample image:
What am I doing wrong or not understanding here?

Unity is returning material color slightly wrong

I have this mini task in my game where you need to click trophies to change color of the wood on them. I have two arrays of colors, one is an array containing all possible colors and the other one contains four colors (the answer) as follows:
I've double checked that the colors are equal between the two arrays. For example the purple in Colors-array has exactly the same r, g, b & a values as the purple in the Right Order-array.
To check whether the trophies has correct color I just loop through them and grab their material color. Then I check that color against the Right Order-array but it's not quite working. For example when my first trophy is purple it should be correct, but it's not because for some reason Unity is returning slightly different material color than excepted:
Hope somebody knows why this is happening.
When you say, they are exactly same color, I assume you are referring rgb values from Color Inspector, which are not precise values.
Now I dont know what could be causing in different values of colors but
You can write an extension method to compare the values after rounding them to closest integer.
public static class Extensions
{
public static bool CompareRGB(this Color thisColor, Color otherColor)
{
return
Mathf.RoundToInt(thisColor.r * 255) == Mathf.RoundToInt(otherColor.r * 255) &&
Mathf.RoundToInt(thisColor.b * 255) == Mathf.RoundToInt(otherColor.b * 255) &&
Mathf.RoundToInt(thisColor.g * 255) == Mathf.RoundToInt(otherColor.g * 255);
}
}
usage:
Color red = Color.Red;
red.CompareRGB(Color.Red); // true;
red.CompareRGB(Color.Green); // false;
Hope this helps.
I would use a palette. This is simply an array of all the possible colors you use (sounds like you have this). Record, for each "trophy", the INDEX into this array, at the same time you assign the color to the renderer. Also, record the index for each "button", at the same time you assign the color to the renderer.
Then you can simply compare the palette index values (simple integers) to see if the color matches.

Is there a better way than using a switch statement to access different properties of a variable in swift?

I have this code that uses a for loop to iterate through every pixel of an image and then based on a user choice changes the value of the red, green or blue channel.
I am using a switch statement to select the channel and the iterate through every pixel in a for loop for that pixels appropriate color property. It feels very cludgy but I can't think of a way to simplify it.
Is there another way to access the property so I can eliminate the switch statement and just have one for loop that select the right pixel color based on user choice?
func applyFilterTo(image:UIImage,forColor:String, withIntensity:Int) -> RGBAImage {
var myRGBA = RGBAImage(image:image)!
switch forColor {
case "RED":
for x in 0..<myRGBA.width {
for y in 0..<myRGBA.height {
let pixelIndex = y * myRGBA.width + x
var pixel = myRGBA.pixels[pixelIndex]
let newValue = Double(pixel.red) + (Double(withIntensity)/100)*Double(pixel.red)
myRGBA.pixels[pixelIndex].red = UInt8(max(0,min(255,newValue)))
}
}
...
}
the above repeats for other options but the only thing that changes is the property I choose (red, blue, green). Better way to do this?
A good way to do this is replacing switch statements with multiple dispatch, such as the Visitor Pattern. Depending on how many options you will have in the switch statement, the switch statement is the easiest to read and understand.

Issue reading screen pixels for color detaction

I am making a game representing freehand drawing and sprites to animate when pass over it. So i have to use color detection and cause an event when the change in color is encountered by sprite on the screen from where it passes. For this i am using glReadpixel() passing RGBA_8888 and GLES20 version and recieve its value in Red Green Blue form but everytime it returns everything to be 0. Tried to change pixelformat and make many hit and trial but no sucess. Can you please help
My code:
`
ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4);
PixelBuffer.order(ByteOrder.nativeOrder());
PixelBuffer.position(0);
int mTemp = 0;
GLES20.glReadPixels(100, 100, 1,1,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE, PixelBuffer);
byte b[] = new byte[4];
PixelBuffer.get(b);
Log.e("COLOR", "R:" + PixelBuffer.get(0) + PixelBuffer.get(1) + PixelBuffer.get(2));
`
Result
Logcat : COLOR R: 000.
I tried using non black background and have red color on screen coordinate provided.
Thanks in advance