a black image's color can not be changed in Unity - image

the color of a black image , or the black part of a image, can not be changed.
I set the image's color to red, but the image is still black.
Is that a desired feature?
What i want is , the image is changed to red.
the Unity version is 5.0.1f.
I am using the new UI.

To understand the source of the problem, you have to understand how "changing colors" work. It's nothing but a simple multiplication. In RGB terms, "black" is a vector of (0, 0, 0) — and it's pretty obvious, that whatever you multiply the 0 by, it stays black.
If you want the template image to be able to change to any color, use white.
To modify colours in a more complex way, you have to understand how the Color property of a Image component works. UI system hides a lot of complexity underneath (and it's good). Basically, Color modifies the vertex colours of a mesh. Since you don't usually specify a material, a default sprite material is used, and it uses a default sprite shader. And inside this shader, when it paints the pixels on the screen, it multiplies the vertex colours by the texture colour sampled at this pixel, and that's how it produces the end result. If you want the colours to be combined in a different way, you'll have to write a custom shader — which is really not as hard, but you probably don't need it for what you're trying to do in the scope of this question.

Black stays black.
White, however, will change with whatever Color you choose, so if you use an image editor and make your image be white instead of black, the image can then be whatever color you choose within Unity.

Check to see if your image has an alpha channel. You can do this with GIMP or Photoshop. If it does, then check your shader/material to ensure that it's set to transparent/diffuse (and not just diffuse). Diffuse only doesn't apply the alpha property to the material.
Also, it doesn't look like you assigned a material...

Make sure you have used yourImage.canvasRenderer.SetColor.
For example:
damageAnim.canvasRenderer.SetColor (new Color (255, 255, 255, 255));

Related

How to change white background to transparent in golang?

How to change a Golang image.Image's white background to transparent?
I want to put the white background into a translucent color, do you have any suggestions?
This is not possible in general but there are heuristic approaches to attempt this.
Why is it not possible?
If the original image had some transparent pixels and was rendered on a plain white background (or any known background) then in the resulting image there is lost information. For example, is a pink pixel created from a red pixel with some transparency or a truly pink pixel without transparency?
Even a pure white pixel may have been fully white originally, or fully transparent, or somewhere in between.
Heuristic approaches
If you know something about the original image or can infer something by looking at it you may be able to deduce what colour and transparency of the original pixels was. A simple first step is that if the outside pixels are white then you might want to flood fill this area with fully transparent pixels. Also white areas inside coloured areas may be holes that can also be transparent.
Next you can infer the colours of border pixels around objects of a single colour (assuming transparency was used for anti-aliasing). For example for a red circle with pink pixels on the circumference it's likely they were originally red with some transparency leading them to be rendered in different shades of pink. For more complex, multi-coloured shapes you can get a reasonable result by manually editing the image using judgement, knowledge/guesswork of the original image and knowledge of how anti-aliasing was performed.
I wrote a Paint.Net plugin (C#) to automate this sort of thing many years ago and sometimes it gave reasonable results, but often you need to do it by hand.
package "image" has Decode function
func Decode(r io.Reader) (Image, string, error)
image.Image is interface
type Image interface {
// ColorModel returns the Image's color model.
ColorModel() color.Model
// Bounds returns the domain for which At can return non-zero color.
// The bounds do not necessarily contain the point (0, 0).
Bounds() Rectangle
// At returns the color of the pixel at (x, y).
// At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
At(x, y int) color.Color
}
after you decode you file into image.Image, you can get Height and Width from Bounds(),and color from At(x,y).
So, here is what to do next:
make another image with alpha channel,
Iterate throw whole image one pix by another,
check color, copy color to new image after you do whatever you want.

Why does Angular Material use alpha to create grey colors?

I've noticed that in angular material when they need a grey they take black and then adjust it's alpha to be lighter. For example if you go here: https://material.angular.io/components and inspect any of the cards on the page, you'll notice the body paragraph has the font color rgba(0, 0, 0, .54);. Does anyone know why they choose to create greys by taking black and adjusting the alpha this way as opposed to just using hex or rgb? The only thing I can think of is that it helps support the warm and cool themes.
From https://material.io/guidelines/style/color.html#color-usability
Do: Using opacity instead of shifting colors often creates better contrast and relative luminance. For example, gray text (#727272) becomes hard to read if the background color changes to magenta.
Don't: Shifting to gray instead of decreasing contrast on black often lowers the relative luminance, making typography unreadable.

Setting the background to transparent using GIMP

Using GIMP 2, I have an image of a grey chair on a white background, as below:
I now want to set the background to transparent. Therefore, I decided to use GIMP's "Color To Alpha" tool. So, I told it to set all pixels which are white (255, 255, 255) to transparent, as below:
This did set all white pixels to transparent. However, it also set the grey pixels on the chair to be partially transparent, as below:
So when I export this image and place it in front of a background, there is no white box around the chair -- but the background partially shows through the chair.
What am I doing wrong?
First, this question is offtopic here, and should be on https://graphicdesign.stackexchange.com .
Second, it is trivial enough just to answer: the color to alpha plug-in is not there to turn a single color, as seem on the image, to transparency: it is a sophisticated plug-in that will remove one color of your image in a way that, if you lace the new image over a background of the same color the color you removed, you get the original image back.
Thus, in your case, it removed the "whiteness" of your chair, transforming all pixels to different opaque shades of black - so that when placed over white, you get the original image.
To simply remove the white, you have to cick on the Select By Color tool (by default th 5th icon on the toolbox), click on the white background to have it selected, and then just edit->cut. (It won't work if your image layer does not have transparency to start with - if that is the case, prior to edit->cut do Layer->Transparency->Add Alpha Channel).
If you get aliased borders, then, after edit>cut, but prior to dismissing your selection, you can do Select->Border... by 1 or 2px, and then use the color to alpha filter with White on this selection.
For more information on Color to Alpha, I have this other answer its use and comparison with edit-cut here: https://graphicdesign.stackexchange.com/questions/28058/gimp-color-to-alpha-is-not-selectable/28097#28097
Just use a selection to restrict the action of Color to alpha where it matters: backgroundand edge pixels:
Select background with fuzzy select
Select>Grow by one pixel so that the selection ofverlaps the edge pixels
Color>Color to alpha

SceneKit: Is it possible to cast an shadow on an Transparent Object?

i am trying to cast an shadow on an totally transparent plane in SceneKit on OSX. I am struggling with this problem since several hours and do not come to any solution.
My Purpose is to generate an Screenshot of several objects with an transparent background and just the shadow on an invisible Plane.
Do you have any suggestions for me how i can make this with apples SceneKit?
Do i have to program my own shader, can i make this work with shadermodifiers or can i use built in functionallity?
UPDATE:
I find an alternative solution for anyone who needs:
create a white plane under 3D model, note that the color of plane must be pure white.
set blend mode of plane's material to SCNBlendModeMultiply.
set light model of plane's material to SCNLightingModelLambert.
This works because any color multiply white color (1, ,1, 1) return itself And lambert light model will not take account of directional light, So the plane will always be background color which look like transparent. Another benefit of this solution is you don't need change light‘s shadow rendering mode.
For people who used to inspector of Xcode.
According to SceneKit: What's New.
First, add a plane under you model. Then prevent it from writing to colorBuffer.
Second, change your light model's shadow rendering mode to deferred. Notice that you must use light which can cast shadows.
Oily Guo, your solution works. Here the solution is in code:
Configuration of the light source:
light.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
light.shadowMode = .deferred
And for the floor (ie. SCNFloor underneath your objects):
material.diffuse.contents = UIColor.white
material.colorBufferWriteMask = SCNColorMask(rawValue: 0)
I do not have an answer to your question, however I have a workaround:
Render your scene and keep the image in memory
Change all the materials in your object for pure black, no specular
Change the plane and the sky to a fully white material, lights to white
Render the scene to another image
On the second image, apply the CIColorInvertand CIMaskToAlpha Core Image filters
Using Core Image apply the Alpha Mask to the first render.
You'll get an image with a correct Alpha channel, and transparent shadows. You will need to tweak the materials and lights to get the results you want.
The shadow may become lighter on the edges, and the only way around that is rendering it as yet another image, and filling it with black after the Mask to Alpha step.

Best natural way of coloring an icon/sprite

I wanna to color a sprite/icon with a transparent background and with shadows. I tried to shift the hue to all pixels but it looks not so natural and I have problems with the black and the white colors in an image. If an image tend to be black shifting the hue do not change the black in red or another color even shifting by 360 degrees.
Tried to color addicting and subtracting color and even in that case the black and the white tend to be colored or disappears at all.
Maybe should I put an image on the icon to achieve the coloring effect ?
Any suggestions on how to proceed.
I lost.
You've been asking a lot about this hue shifting thing, so I figured I'd try to work out an example: http://jsfiddle.net/EMujN/3/
Here's another that uses an actual icon: http://jsfiddle.net/EMujN/4/
There's a lot in there. There's a huge data URL which you can ignore unless you want to replace it. Here's the relevant part where we modify HSL.
//SHIFT H HERE
var hMod = .3;
hsl[0]=(hsl[0]+hMod)%1;
//MODIFY S HERE
var sMod = .6;
hsl[1]=Math.max(0,Math.min(1,
hsl[1]+sMod
));
//MODIFY L HERE
var lMod = 0;
hsl[2]=Math.max(0,Math.min(1,
hsl[2]+lMod
));
I've converted to HSL because it's a lot easier to accomplish what you want in that color space than RGB.
Without getting any more complex, you have three variables you can tune: how much to add to either Hue, Saturation, or Lightness. I have the lightness variable set to 0 because any higher and you will see some nasty JPEG artifacts (if you can find a decent .png that would be better, but I went with the first CC night image I could find).
I think the hue shift (yellow to green) looks pretty good though and I have maxed out the saturation, so even a normally white light appears bright purple. Like I said in my comment, you will need to increase the lightness and saturation if you want to colorize patches of black and white. Hopefully, you can figure out what you need from this example.
image used: http://commons.wikimedia.org/wiki/File:Amman_(Jordan)_at_night.jpg
I found a better solution by myself which can solve the problem with the black and white.
So basically the solution can be solved in multiple steps. Here I will define the steps. Later I'll provide some working code:
Get the image
Calculate the predominant color, averaging the image pixels or simply providing an input RGB value which is the predominant that your eye can catch.
If the predominant tends to be black or white, or both, the image has to be recolored with an addictive or subtractive method, addictive if black, subtractive if white. So basically all RGB pixels should be attenuated or sharpened until RED. I think that the best solution should be RED, because RED is first in the HUE scale, and this can help when we will hue-shift the pixels.
To have a unique algorithm which can work with different kind of images, not only black predominant or white, ideally the input the non-black and non-white predominant images should be pre-hueshifted manually, using photoshop or with another algorithm in a way that the new predominant color results to be RED too
After that the Hue shifting coloring is straighforward. We know that the predominant color is RED for all the images, and we'll shift the HUE values with a difference between the HSV value of the desired color and the HSV of the predominant color (RED).
Game over. We have a pretty universal way to color different images with hue shifting in a natural way.
Another question could be how to authomatically pre-shift the input images which predominant color is not black or white.
But this is another question.
Why this coloring method could be considered natural. Simply consider one thing. Generally the non dominant black or white colors are part of the shadows and light which gives a 3D feel to the images. On the other hand if my shoes are 100% black and i will tint them with some colors, they will no more be black. Color the dominant black cannot be achieved simply shifting the HSV parameters but other steps should be performed. The steps are the above described.

Resources