I'm relatively new to Programming and im trying to make a Puzzle Boardgame in Java, and I want to make the tokens you have to move in different colors.
The Position and Number of the tokens is saved in this array:
{
{2,1,1,3},
{2,1,1,3},
{4,6,6,5},
{4,7,8,5},
{9,0,0,10}
}
So now my question is, how can i convert these numbers into RGB color values, that are easily distinguishable ?
You can Use the java.awt.Color class for Rgb and int Stuff.
http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
You can use:
http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(int)
for getting an Color from an int or
http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(int,%20int,%20int)
for creating an Color based on an
rgb value.
You can use an Random method for generate the Colors.
Or choose some Colors and store them in an Array and pick them randomly for any Token.
Edit (Response to Comment):
You can take an hashmap for this.
So save the token number as the key and the Color as the Value:
HashMap<Integer,Color> map = new HashMap<Integer,Color>();
and than just simple write an Method like this to get the Color of the Token:
private Color getColor(int tokenNumber){
if(!map.containsKey(tokenNumber)){
map.put(tokenNumber, createColorForToken(tokenNumber));
}
return map.get(tokenNumber);
}
sorry for bad English :P
Related
How to convert HEX Color String to RGB or RBGA in Unreal Engine's Blueprint. For example: #f0f8ff to 240/248/255 RGB vector.
Thanks a lot.
Since the Blueprint API does not support Hex to RGB or RGB to Hex, but the regular FColor struct does, I'd suggest, you write yourself a wrapper for it in a UBlueprintFunctionLibrary.
I won't go into detail how to create one of these libraries, since you can find an easy tutorial in the Unreal Wiki. However, I may give you the code that will hopefully work:
part of the header:
/** Converts hex string to color. Supports formats RGB, RRGGBB, RRGGBBAA, RGB, #RRGGBB, #RRGGBBAA */
UFUNCTION(BlueprintCallable, Category="YourFunctionLibrary")
static FColor HexToColor(FString HexString);
/** Converts color to hex string */
UFUNCTION(BlueprintCallable, Category="YourFunctionLibrary")
static FString ColorToHex(FColor Color);
part of the compilation unit:
FColor YourFunctionLibrary::HexToColor(FString HexString)
{
return FColor::FromHex(HexString);
}
FString YourFunctionLibrary::ColorToHex(FColor Color)
{
return Color.ToHex();
}
Haven't tried it yet, but hope it works!
I had a similar question while building an editor utility and found another option that worked out well. This utilizes a python command.
Here's what I've done:
https://blueprintue.com/blueprint/syz1siaw/
It's not the most elegant, but it's fun!
I need to compare colors. I want to set a color into a variable and then compare that it a value obtained using getPixel.
However, the following does not work. It seems like ImageJ does not know that the value in basecolor is a color.
basecolor = 0xFFFFFF;
rightpixel = getPixel(x, y);
if (rightpixel == basecolor) count++;
Your problem is in getPixel which does not yield a color written in hex.
I present to you your best friend on ImageJ : the built-in macro functions code
https://imagej.nih.gov/ij/developer/macro/functions.html
which documents built-in pixel functions such as getPixel().
For getPixel(), it is stated "Note that pixels in RGB images contain red, green and blue components that need to be extracted using shifting and masking. See the Color Picker Tool macro for an example that shows how to do this. ", and the Color Picker Tool macro tells us how to go from color "bits" to RGB.
So if you wish to compare colors, you do :
basecolor=newArray(0,0,0);
rightpixel = getPixel(x,y);
//from the Color Picker Tool macro
//converts what getPixel returns into RGB (values red, green and blue)
if (bitDepth==24) {
red = (v>>16)&0xff; // extract red byte (bits 23-17)
green = (v>>8)&0xff; // extract green byte (bits 15-8)
blue = v&0xff; // extract blue byte (bits 7-0)
}
//compare the color with your color
if(red==basecolor[0] && green==basecolor[1] && blue==basecolor[2]){
print("Same Color");
count++;
}
//you can also work with hex by converting the rgb to hex and then
//comparing the strings like you did
I need help with Matlab.
I have an Excel sheet with three columns: X, Y and Z. I have used plot3 function to make one 3D curve.
But I need to vary it in colors.
What function/functions do I need to make X, Y and Z in different 3 colors(each column one color)?
Could you please send me link, where I can find out the way, or just write the function/functions needed for it?
Here is the code:
VCG=xlsread('VCGsheet.xls');
figure(1)
plot3(VCG(:,1),VCG(:,2),VCG(:,3));
grid on
I know that plot3 isn't suitable for it.
I'm not aware of a native command that draws line with varying color.
I will also assume that VCG vector is a vector of RGB values, so one color per row.
RGB = VCG(:,1:3);
If you want, you can replace it (VCG(:,1:3)) with any other vector of color derived from your data. Here is an example if you have a single value that can be calculated for each of your points, in the vector T for example, and that you want to show as a color.
map = jet(256);
RGB = map(round((T(:)-min(T(:)))/(max(T(:)) - min(T(:)))*255)+1,:);
For the plot, I propose two different ways:
you can make use of the scatter3 function
It will print points, but no lines connecting the points. The color vector is set with VCG(:,1:3)
colormap('jet')
scatter3(VCG(:,1), VCG(:,2), VCG(:,3), 70, RGB, 'filled');
You can make a direct use of line in a for loop.
It is a bit slower, but it is generally ok for graphs.
for i=2:size(VCG,1)
line(VCG(i-1:i,1), VCG(i-1:i,2), VCG(i-1:i,3), 'color', RGB(i-1,:));
end
If you want both, just use the hold function
I hope I understood what you needed !
Can anyone tell me how to go about converting a RGB Image object to Gray Scale? I know there is a lot of information on how to do this in Java already, but I just wanted to get an answer specific to Codenameone so others can benefit.
I am trying to implement image binarization using Otsu’s algorithm
You can use Image.getRGB() then modify the array as explained in this answer:
Convert Image to Grayscale with array matrix RGB java
Notice that the answer above is a bit over simplistic as it doesn't take into account the correct weight per color channel for proper grayscale effect but this depends on your nitpicking levels.
Then use this version of createImage with the resulting array.
For anyone looking for a simplified way (not using matrices) of doing what Shai is hinting, here is some sample code
int[] rgb = image.getRGB();
for(int k = 0;k<rgb.length;k++)
{
if(rgb[k]!=0)
{
int r = rgb[k]/256/256;
rgb[k]=rgb[k]-r*0x10000;
int g = rgb[k]/256;
rgb[k]=rgb[k]-g*0x100;
int b = rgb[k];
int intensity = (int)Math.round(((r+g+b)/(256.0*3.0))*256);
rgb[k] = intensity+(intensity*256)+intensity*(256*256);
}
}
Image grayImage = Image.createImage(rgb,image.getWidth(),image.getHeight());
Given a starting hex code, I would like to know the maths to calculate the linear values of lightness in ascending and descending order. Same for Hue and Saturation.
It's kinda difficult for me to describe exactly what i want, forutnately i've found this page which make use of the exact algorithms i need:
http://www.workwithcolor.com/hsl-color-schemer-01.htm
If you checked the page you noticed that the last 3 redio buttons read: Linear by Hue, Linear by Saturation, Linear by Lightness. Each, gives you a list of hex codes in ascending order that correspond to the original hex code.
For example, for the lightness they give the following list (from color FFCE2E):
FFCE2E FFDA61 FFE694 FFF2C7 FFFEFA
I need the formulas, please.
Thanks in advance.
You can mash this up from multiple places. In a nutshell you need:
The HSL value of your picked color. Maybe this is obtained by converting an RGB to HSL (How do you get the hue of a #xxxxxx colour?) or on the website you just pick it on a palette
Now you have the 3 component (H, S, and L) and depending on which checkbox you choose, you start to decrement the component by the % value given in the edit box.
You'll obtain a list of values during this decrement and you'll now do a reverse conversion from the HSL value to the RGB (HSL to RGB color conversion).
// I gonna use rgbToHsl and hslToRgb from https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
var initialRGB = [ir, ig, ib];
var initialHSL = rgbToHsl(initialRGB[0], initialRGB[1], initialRGB[2]);
var howManyVariants = 4;
var decrementPercent = 0.1; // 10%
// This example is for hue change
var decrement = initialHSL[0] * decrementPercent;
for (var i = 0; i < howManyVariants; i++) {
// Linear decrementation
var nextHue = initialHSL[0] - i * decrement;
var nextColor = hslToRgb(nextHue, initialHSL[1], initialHSL[2]);
// visualize somehow
}
Similarly, if you want to have a set of variation by saturation then you decrement only the second parameter/component, and if you want vary luminescence, you vary the 3rd parameter.
Hope this is clear.