How to assign random colors to a gameObject in unity? - unity3d

I have 3 colors ( Red, Green, and Yellow ), I want to assign random colors from these 3 colors to my Ball every time when My game starts or restarts after gameOver.
Thanks!

Define the colors in the list and select them in the inspector.
public List<Color> Colors = new List<Color>();
private void ChangeColor()
{
GetComponent<MeshRenderer>().material.color = Colors[Random.Range(0, Colors.Count)];
}

Create a list or array with the colors, then use random.range() accordingly on that array in the Start method (or a custom method if you restart manually.)
If every color should be guaranteed unique, use the singleton pattern to keep a common instance of that colors list and remove the used color every time it's taken.

Related

Generate a 2 random color on the same text

I'm new in a flutter. I can't figure out how to generate the 2 random colours on the same Text(). Can anyone help me out? Really appreciate.
String data = "+RM67.80";
var value = data.substring(0,1); // it will get the first character
After that you can check the condition if value == "+" apply green color otherwise apply red.

ECharts: dataset with bars colored based on category name?

I'd like to be able to control bar colors based on xAxis/category label name, throughout multiple charts, while using the dataset.source option of feeding data.
I'm guessing the solution might involve using visualMap?
customColors = {
x1: '#123',
x2: '#456',
x3: '#789',
...
}
I don't think visualMap is the way to do this, you probably want to set the colors in series:
https://echarts.apache.org/en/option.html#series-bar.data.itemStyle.color
or define a color palette:
https://echarts.apache.org/en/option.html#color
Echarts should color the chart by series by default:
https://echarts.apache.org/en/option.html#series-bar.colorBy

SSRS: Custom colors for the Category axis of a stacked bar chart

I have a stacked bar chart that only ever has 5 categories(but the value of the categories change from year to year, it is a sliding 5 year window).
I have successful customised the bars to the colors I want.
But now I wish to make the label of each Category the same color as the customised bar color.
Is there a way to do this?
You can use custom code for this.
In Report Properties | Code, you can paste in the following code:
Private colourPalette As String() = {"#418CF0", "#FCB441", "#DF3A02", "#056492", "#BFBFBF", "#1A3B69", "#FFE382", "#129CDD", "#CA6B4B", "#005CDB", "#F3D288", "#506381", "#F1B9A8", "#E0830A", "#7893BE"}
Private count As Integer = 0
Private mapping As New System.Collections.Hashtable()
Public Function GetColour(ByVal groupingValue As String) As String
If mapping.ContainsKey(groupingValue) Then
Return mapping(groupingValue)
End If
Dim c As String = colourPalette(count Mod colourPalette.Length)
count = count + 1
mapping.Add(groupingValue, c)
Return c
End Function
This will give you the option of the pastel colour palette. If you want other colours, simply replace the hex colour codes with values of your choice.
To use this, simply use the following expression:
=Code.GetColour(Fields!Thingy.Value)
Use this on your series and your label fill expressions. This will ensure that the same colour appears for both. If you have multiple graphs with the same values in, this will also ensure that the same data series across multiple graphs always have the same colour.

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.