Vbnet Convert Color to Red Green and Blue

  1. #1

    Michel Jr is offline

    Thread Starter

    Addicted Member Michel Jr's Avatar


    Question

    Hi,

    How can I extract the value of Red, Green and Blue
    that composes an specific color?

    Example:

    Suppose the variable vColor stores the color below:
    vColor = &H80000014

    How can I find the value of Red, Green and Blue that
    I should use in RGB(999,999,999) to produce the same color?

    Thanks

    Michel Jr.


  2. #2

    Code:

    'Very useful code to convert long to rgb, rgb to long, rgb to hex and qbcolor to rgb  Public Red As Integer Public Green As Integer Public Blue As Integer   Public Sub ConvertQBColorToRGB(QBColorValue As Integer)      If QBColorValue < 0 Or QBColorValue > 15 Then MsgBox "There's error in your QBColor.", vbCritical, "Error"      ConvertLongToRGB (QBColor(QBColorValue))  End Sub   Public Function ConvertRGBToHex(RedColor As Integer, GreenColor As Integer, BlueColor As Integer)      On Error GoTo ErrorInLongColor      ConvertRGBToHex = Right(0 & Hex(RedColor), 2) & Right(0 & Hex(GreenColor), 2) & Right(0 & Hex(BlueColor), 2)     Exit Function  ErrorInLongColor:     MsgBox "There's error in your RGB color.", vbCritical, "Error"  End Function   Public Sub ConvertLongToRGB(Color As Long)      On Error GoTo ErrorInLongColor      Blue = Color \ 65536     Green = (Color - Blue * 65536) \ 256     Red = Color - (Blue * 65536) - (Green * 256)      Exit Sub  ErrorInLongColor:     MsgBox "There's error in your long color.", vbCritical, "Error"  End Sub   Public Function ConvertRGBToLong(RedColor As Integer, GreenColor As Integer, BlueColor As Integer)      On Error GoTo ErrorInLongColor      ConvertRGBToLong = RGB(RedColor, GreenColor, BlueColor)     Exit Function  ErrorInLongColor:     MsgBox "There's error in your RGB color.", vbCritical, "Error"  End Function

  3. #3

    PaulLewis is offline

    Hyperactive Member


    Talking Cheeky method...

    OK, not chheky exactly, but a bit different to Matthew's and with no error checking etc!

    in a module, declare the following

    Code:

    Public Type MyRGBColor     Red As Byte   green As Byte   blue As Byte End Type  Public Type MyColor   RGB As Long End Type
    Now, in any code where you want to "convert" from an RGB color to Long or vice versa, just do the following:

    Code:

    ' RGB to Long   Dim mycol As MyColor   Dim rgbcol As MyRGBColor   rgbcol.red = 200   rgbcol.green = 30   rgbcol.blue = 220   lset mycol = rgbcol    debug.print mycol.rgb  ' Long to RGB   Dim mycol As MyColor   Dim rgbcol As MyRGBColor   mycol.rgb = 14425800   lset rgbcol = mycol   debug.print "Use RGB(" & rgbcol.red & "," & rgbcol.green & "," & rgbcol.blue & ")"
    Of course, you could easily make error checking conversion functions to handle this for you, but this method is relying on the lset command to "convert" for us.

    Nice eh?

    Paul Lewis


  4. #4

    I found a better code .

    Code:

    Public Enum RGBColor     gcRed = 1     gcBlue = 2     gcGreen = 3 End Enum  Public Function GetRGBColor(ByVal Color As String, ColorPart As RGBColor) As Long     Dim strColor As String     Select Case ColorPart         Case gcRed             strColor = Right$("000000" & Hex$(Color), 6)             GetRGBColor = Val("&h" & Right$(strColor, 2))         Case gcBlue             strColor = Right$("000000" & Hex$(Color), 6)             GetRGBColor = Val("&h" & Left$(strColor, 2))         Case gcGreen             strColor = Right$("000000" & Hex$(Color), 6)             GetRGBColor = Val("&h" & Mid$(strColor, 3, 2))     End Select End Function

  5. #5

    Seems like you guys like to make it look complicated

    Code:

    Red=Color mod 256 Green=int(Color/256) mod 256 Blue=int(Color/&H10000)

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.


  6. #6

    Darkbob is offline

    Hyperactive Member


    Re: Seems like you guys like to make it look complicated

    Quote Originally Posted by kedaman View Post

    Code:

    Red=Color mod 256 Green=int(Color/256) mod 256 Blue=int(Color/&H10000)
    OK that's genius. Thanks very much. Helped me with a control that uses 3 stupid RGB numbers rather than Hex.

  7. #7

    Re: how to find the Red, Green Blue ( RGB()) value of a color?

    Darkbob, replying to a 2-decade old thread

    Anyway, this is better because it doesn't use the Mod() operator or Int() or non-integer division. It's more efficient/faster

    Code:

    Red = (Color And &HFF&)  ' take lower byte Green = (Color And &HFF00&) \ &H100&  ' shift next byte to lower & take that Blue = (Color And &HFF0000) \ &H10000  ' shift next byte to lower & take that
    When you print out a color in hex, keeping 6-8 characters, and then look at above code, you can see how simple the formula really is

    Edited: No offense towards kedaman (posted the code), that is probably the worst/slowest way to get the individual RGB values. Why? Using doubles throughout the formula (due to calculations) & doubles are slower than longs. Bitshifting is among the quickest operations as shown in my example. Additionally, and this doesn't apply with all applications, kedaman's formula fails miserably if an alpha byte above 127 exists in the color, i.e., this value for vbGreen with 100% opacity: &HFF00FF00

    Last edited by LaVolpe; Feb 2nd, 2019 at 10:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Full Width

stewartapans1977.blogspot.com

Source: https://www.vbforums.com/showthread.php?21921-how-to-find-the-Red-Green-Blue-(-RGB())-value-of-a-color

0 Response to "Vbnet Convert Color to Red Green and Blue"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel