הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, December 31, 2008 6:35 PM
Hi, I'd like to know how to convert argb to rgb
All replies (12)
Wednesday, December 31, 2008 7:00 PM ✅Answered | 1 vote
The alpha component is located in bits 24-31 of the rgb value.
You can:
1 | rgb = rgb AND &H00FFFFFF |
2 | Label1.Text = Hex(rgb) |
3 |
Regards
-- Don't forget to close the thread by marking the correct post(s) as ANSWERED!
Wednesday, December 31, 2008 7:10 PM ✅Answered | 1 vote
Hi cesno,
Add a Button1 and try this code:
Public Class Form1 |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click |
Label1.Text = Chr(Asc(Mid(Label1.Text, 3))) & Chr(Asc(Mid(Label1.Text, 4))) & Chr(Asc(Mid(Label1.Text, 5))) & Chr(Asc(Mid(Label1.Text, 6))) & Chr(Asc(Mid(Label1.Text, 7))) & Chr(Asc(Mid(Label1.Text, 8))) |
End Sub |
End Class |
Bill
Wednesday, December 31, 2008 7:12 PM ✅Answered
Works, Thank you , but you missed quotes in ur code Marcel ;]
rgb = rgb And "&H00FFFFFF"
Thank you too Bill
Thursday, January 1, 2009 5:45 PM ✅Answered | 1 vote
'KISS |
Dim someColor As Color = Color.Purple |
Dim a As String = Convert.ToString(someColor.A, 16).PadLeft(2, "0"c).ToUpper 'alpha |
Dim r As String = Convert.ToString(someColor.R, 16).PadLeft(2, "0"c).ToUpper 'red |
Dim g As String = Convert.ToString(someColor.G, 16).PadLeft(2, "0"c).ToUpper 'green |
Dim b As String = Convert.ToString(someColor.B, 16).PadLeft(2, "0"c).ToUpper 'blue |
Debug.WriteLine("RGB of Purple in Hex " & r & g & b) |
Debug.WriteLine("Alpha of Purple in Hex " & a) |
Dim argb As Integer = someColor.ToArgb And &HFFFFFF |
Debug.WriteLine(Convert.ToString(argb, 16)) 'to hex the .Net way |
'debug output |
'RGB of Purple in Hex 800080 |
'Alpha of Purple in Hex FF |
'800080 |
From MSDN documentation
"Remarks
The byte-ordering of the 32-bit ARGB value is AARRGGBB. The most significant byte (MSB), represented by AA, is the alpha component value. The second, third, and fourth bytes, represented by RR, GG, and BB, respectively, are the color components red, green, and blue, respectively."
Thursday, January 1, 2009 5:49 PM ✅Answered | 1 vote
To see why...
Option Strict
By default the IDE sets a particular compiler option to Off:
Option Strict Off.
MSDN Library: "Option Strict"
This means that the compiler will try to convert one type to another - it will add code to do so. Consider:
Option Strict Off |
Public Class Form1 |
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click |
Dim i As Integer = "hello World" |
Me.Text = i |
End Sub |
End Class |
This is dumb code. What is the numeric value of "hello world"?
If you run this, and click the form, you will get an exception:
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
To see what the code is doing, you need to dig deep. .Net code is like Java code - it is first compiled to an intermediate language. This intermediate language is then run when the user runs the program. The intermediate language is converted into the actual instructions that the computer understands.
You can look at the intermediate language using the ILDASM tool. Here's the Intermediate language for the above Form1_Click procedure:
// Code size 28 (0x1c) |
.maxstack 2 |
.locals init ([0] int32 i) |
IL_0000: nop |
IL_0001: ldstr "hello World" |
IL_0006: call int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(string) |
IL_000b: stloc.0 |
IL_000c: ldarg.0 |
IL_000d: ldloca.s i |
IL_000f: call instance string [mscorlib]System.Int32::ToString() |
IL_0014: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Form::set_Text(string) |
IL_0019: nop |
IL_001a: nop |
IL_001b: ret |
I don't understand it all. But, I can see that the string "hello world" is loaded into memory. Then it calls an external function to convert the string into an Integer - the ToInteger(String) method.
If instead of "hello world" we use "10", then the program compiles, and also runs without an error:
Option Strict Off |
Public Class Form1 |
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click |
Dim i As Integer = "10" |
Me.Text = i.ToString |
End Sub |
End Class |
This time the IL bit of interest is:
IL_0001: ldstr "10"
IL_0006: call int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(string)
So, again it loads the string and calls the ToInteger method to convert the string to an Integer. After all i is declared as an Integer so it can only store an integer value - not a String.
If you switch Option Strict On, then it will NOT compile. Option Strict means that you have to make any casts like that explicitly and not rely on the compiler to add code to make the conversions for you.
Always use Option Strict otherwise:
- You won't develop an understanding of the types you are using.
- You won't understand the code you are writing.
- You won't always choose a suitable type for variables.
- You will get errors because the compiler is making strange casts.
- Your code could be inefficient.
Whilst you are at it, switch on Option Explicit.
ARGB.
32 bit ARGB colour values are stored using 32 bits of information, 8 bits for each colour channel. A convenient way to store such 32 bit values is inside an Integer - as Integers are also 32 bits in size. Also convenient is the fact that when you represent the Integer using hexadecimal notation, the colour components are simple to read:
&HAARRGGBB - AA is the alpha value, RR is the Red value, GG is the Green value, BB is the Blue value.
So from a glance you can see that
&HFF00FF00 will be Solid green. If you represented the same number with decimal notation then it would be 4278255360 - which doesn't immediately make you think "ah that will be green".
Either representation refers to exactly the same bit sequence - they are all the same number:
&HFF00FF00 is the same number as
4278255360 is the same number as
11111111000000001111111100000000 in binary.
Whilst we have that colour in binary, we can see how the AND operation works:
11111111000000001111111100000000 &HFF00FF00 - with AA information
00000000111111111111111111111111 &H00FFFFFF - the bit mask to remove alpha
AND
00000000000000001111111100000000 &H0000FF00 - The colour with the alpha component set to 0.
Note that this is a Bitwise AND operation.
AND is defined for operations between two NUMBERS.
rgb = rgb And "&H00FFFFFF"
This would not be allowed with Option Strict On - because you can't have AND between an Integer and a String - it makes no sense - just as assigning "hello world" to an integer makes no sense.
The compiler is jumping in and trying to save the day by converting your String to something else. In this case, AND is defined between Integer and Integer, but the compiler decides to use the AND that is defined between Long and Long. So the String is converted into a Long, and then ANDed with rgb, and then converted back into an Integer. String->Long->Integer
Without the quotes, &HFFFFFF is just treated as an Integer anyway. There is no need to make any conversion. The AND operation between two Integers is used. It makes sense, it is efficient.
There are three ways to represent Integers in Code -
Just as you can say:
Dim i As Integer = 1
You can also say
Dim i As Integer = &H1
And even:
Dim i As Integer = &O1 ' Octal
My preferred way to get the 6 digit hexadecimal string representation of the colour information disregarding the alpha component would be:
Label1.Text = (rgb AND &HFFFFFF).ToString("X6")
Or
Label1.Text = (rgb AND &HFFFFFF).ToString("x6")
For lower case. The advantage over Convert.ToString(x, 16) is that it will pad with 0s automatically up to the value specified (6).
Thursday, January 1, 2009 6:13 PM ✅Answered | 1 vote
'the same |
s = Convert.ToString(aRGB, 16).PadLeft(6, "0"c).ToUpper |
s = aRGB.ToString("X6") |
'however what if i want binary |
s = Convert.ToString(aRGB, 2) |
'or octal |
s = Convert.ToString(aRGB, 8) |
Wednesday, December 31, 2008 6:39 PM
I'm not sure I follow? An ARGB color is an RGB color with an added alpha channel - basically, it's transparency. Can you be a little more clear as to what you're trying to accomplish?
Tom Shelton
Wednesday, December 31, 2008 6:41 PM
Just take the components R,G and B and use them. The A component from an ARGB color value specifies the value's alpha that should be used in blending operations. Otherwise, the alpha does not influence the components. (as would K influence C,M,Y when converting from CMYK to CMY).
Regards
-- Don't forget to close the thread by marking the correct post(s) as ANSWERED!
Wednesday, December 31, 2008 6:47 PM
I want to display hex value of colour (I have code which gets color from pixels) in label1 but it displays it with alpha channel so it is 8 digits long , but I need 6 digits rgb to be able to use it in web, or maybey there is way to cut alpha hex value and display only rgb
Label1.Text = Hex(rgb) , rgb is argb color from selected pixel
Thursday, January 1, 2009 4:51 PM
No, the quotes are not supposed to be there!
Friday, January 2, 2009 10:32 AM
Thank you dbasnett , and jo0ls especially for such
detailed help
Saturday, July 9, 2011 4:56 AM | 1 vote
I don't know what all this other code was about but I found an easy way.
Dim aRGB As Integer = Panel1.BackColor.ToArgb
Dim RED, GREEN, BLUE As Integer
RED = Color.FromArgb(aRGB).R
GREEN = Color.FromArgb(aRGB).G
BLUE = Color.FromArgb(aRGB).B