What I have is R:255 G:181 B:178, and I am working in C# (for WP8, to be more specific)
I would like to convert this to a hex number to use as a color (to set the pixel color of a WriteableBitmap). What I am doing is the following:
int hex = (255 << 24) | ((byte)R << 16) | ((byte)G << 8) | ((Byte)B<<0);
But when I do this, I just get blue.
Any ideas what I am doing wrong?
Also, to undo this, to check the RGB values, I am going:
int r = ((byte)(hex >> 16)); // = 0
int g = ((byte)(hex >> 8)); // = 0
int b = ((byte)(hex >> 0)); // = 255
Try the below:
using System.Drawing;
Color myColor = Color.FromArgb(255, 181, 178);
string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");
Using string interpolation, this can be written as:
$"{r:X2}{g:X2}{b:X2}"
You can use a shorter string format to avoid string concatenations.
string.Format("{0:X2}{1:X2}{2:X2}", r, g, b)
You can use ColorHelper library for this:
using ColorHelper;
RGB rgb = new RGB(100, 0, 100);
HEX hex = ColorConverter.RgbToHex(rgb);
Greetings fellow humans,
//Red Value
int integerRedValue = 0;
//Green Value
int integerGreenValue = 0;
//Blue Value
int integerBlueValue = 0;
string hexValue = integerRedValue.ToString("X2") + integerGreenValue.ToString("X2") + integerBlueValue.ToString("X2");
Related
I've been doing this Kata and I'm a little stuck on finishing this function. I've searched for parsing but haven't found variants that parse shorthand hex's into RGB. So far I have
public static string Rgb(int r, int g, int b)
{
r = Math.Clamp(r, 0, 255);
g = Math.Clamp(g, 0, 255);
b = Math.Clamp(b, 0, 255);
string hexR = Convert.ToString(r, 16);
string hexG = Convert.ToString(g, 16);
string hexB = Convert.ToString(b, 16);
string res = "000000";
res = res.ReplaceAt(0, hexR.Length, hexR);
res = res.ReplaceAt(2, hexG.Length , hexG);
res = res.ReplaceAt(4, hexB.Length , hexB);
return res.ToUpper();
}
It works but single digit hex's arent parsed correctly for example with an input of 212,53,12
the result is D435C0 where as it needs to be D4350C
Would greatly appreciate advice and feedback to the function as I'm trying to improve.
How about
public static string Rgb(int r, int g, int b)
{
r = Math.Clamp(r, 0, 255);
g = Math.Clamp(g, 0, 255);
b = Math.Clamp(b, 0, 255);
int res = r * 256 * 256 + g * 256 + b;
return res.ToString("X");
}
You can use a format string, as mentioned by #Demon in the comments
return $"{r:X2}{g:X2}{b:X2}";
Note that X2 gives it in uppercase, whereas x2 will give lowercase
I'm converting an RGB value to a single integer with the following:
public static int RGBtoInt(int red, int greed, int blue)
{
return blue + green + (green * 255) + (red * 65536);
}
but struggling to write an inverse method taking in an integer and returning the single RGB components.
Something of thematic nature with:
public static Vector3 IntToRgb(int value)
{
// calculations...
return new Vector3(red, green, blue);
}
The Color.FromArgb(int) method isn't creating the RGB colour I need.
The RGBtoInt function above matches the RGB integer values returned by OpenGL and I am looking for a reverse method. It's the same conversion method used here.
The conversion can be done as follows.
public static Vector3 IntToRgb(int value)
{
var red = ( value >> 0 ) & 255;
var green = ( value >> 8 ) & 255;
var blue = ( value >> 16 ) & 255;
return new Vector3(red, green, blue);
}
To my understanding, the initial conversion should be done as follows.
public static int RGBtoInt(int r, int g, int b)
{
return ( r << 0 ) | ( g << 8 ) | ( b << 16 );
}
Try this Color c = Color.FromArgb(someInt);
and then use c.R, c.G and c.B for Red, Green and Blue values respectively
I want to set Border background color from web color value in my windows 8 mobile application .
I found one method that convert hex to Argb but its not working for me ..
private System.Windows.Media.Color FromHex(string hex)
{
string colorcode = hex;
int argb = Int32.Parse(colorcode.Replace("#", ""), System.Globalization.NumberStyles.HexNumber);
return System.Windows.Media.Color.FromArgb((byte)((argb & -16777216) >> 0x18),
(byte)((argb & 0xff0000) >> 0x10),
(byte)((argb & 0xff00) >> 8),
(byte)(argb & 0xff));
}
I am using above method like..
Border borderCell = new Border();
var col = FromHex("#DD4AA3");
var color =new System.Windows.Media.SolidColorBrush(col);
borderCell.Background = color;
But if I pass color hex value like below
var col = FromHex("#FFEEDDCC");
its works fine but it not work on my hex color value.
Before posting this question I go thru this stack answer.
How to get Color from Hexadecimal color code using .NET?
Convert System.Drawing.Color to RGB and Hex Value
Why not simply use System.Windows.Media.ColorConverter?
Color color = (Color)System.Windows.Media.ColorConverter.ConvertFromString("#EA1515");
finally I found one method that return color from hex string
public System.Windows.Media.Color ConvertStringToColor(String hex)
{
//remove the # at the front
hex = hex.Replace("#", "");
byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
int start = 0;
//handle ARGB strings (8 characters long)
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
start = 2;
}
//convert RGB characters to bytes
r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);
return System.Windows.Media.Color.FromArgb(a, r, g, b);
}
Good morning all,
I am making an Image Steganography project for college.
While hiding data in image. I am writing the "Text Length" which is an Int32 in a pixel. As Int32 is of 4 bytes. I thought I could write it in the 4 bytes of Alpha,Red,Green,Blue, as each color is of 1 byte.
Then I save the image in bmp format.
I used single stepping and data get properly distributed and set in the pixel.
The problem arise when I read back the pixel. R,G,B have their value as i had set them. But the alpha is always 255 no matter what it was set.
Code that I am using for distributing Int32 into 4 bytes are
byte R, G, B, A;
int colorValue = messageLength;
int first = colorValue & 255;
//R contains bit 0-7 means the least significant 8 bits
R = (byte)first;
colorValue = colorValue - first;
int second = colorValue & 65535;
colorValue = colorValue - second;
second = second >> 8;
//G contains 8-15
G = (byte)second;
int third = colorValue & 16777215;
colorValue = colorValue - third;
third = third >> 16;
//B contains 16-23
B = (byte)third;
colorValue = colorValue >> 24;
//A contains 24-31
A = (byte)colorValue;
pixelColor = Color.FromArgb(A, R, G, B);
bitmap.SetPixel(location.X, location.Y, pixelColor);
Code for getting the values back is
byte R, G, B, A;
R = pixelColor.R;
G = pixelColor.G;
B = pixelColor.B;
A = pixelColor.A;
messageLength = A;
messageLength = messageLength << 8;
messageLength += B;
messageLength = messageLength << 8;
messageLength += G;
messageLength = messageLength << 8;
messageLength += R;
Is there something I am missing. Is it that BMP does not allow alpha value to persist???
Please help.
Thanks.
Sorry to say that Bitmap doesn't support an alpha value.
I'm using BitConverter.ToInt32 to pack 3 byte values into an int, like so:
byte R = 0;
byte G = 0;
byte B = 0;
int i = BitConverter.ToInt32(new byte[] { R, G, B, 0 }, 0);
Is there a faster way to do this that doesn't involve the creation of a new int each time? Getting the bytes out of an int is easy:
int i = 34234;
byte B = (byte)(i >> 0);
byte G = (byte)(i >> 8);
byte R = (byte)(i >> 16);
Is there a simple way to reverse this process and use bit-shifting to write the RGB bytes back over an existing int?
int i = (B << 0) | (G << 8) | (R << 16);
You ought to consider the Color structure. It has R, G and B properties and FromArgb() and ToArgb() methods.