How can you convert an integer into a VB.NET color?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Homes

    How can you convert an integer into a VB.NET color?

    In VB6, foreground and background colors of controls had to be assigned a
    single number. If you knew the RGB values for the color, you still had to
    convert them into the single number accepatable to the VB6 controls.

    In VB.NET, you can't set colors that way anymore, now you have to use a
    "color". There is a way to convert RGB values to a "color", using the
    Color.FromARGB method. But there doens't seem to be a way to convert the old
    single numbers into a "color".

    In VB6, there was a method to convert rgb values into a single number, using
    the RGB method. This method is still available in VB6 through the
    Microsoft.Visua lBasic.Informat ion namespace. However, I have the single
    numbers I used before (that were REQUIRED in VB6), I don't have separate Red,
    Green, and Blue values for those numbers. Neither VB6 nor VB.NET seem to have
    a method for converting a single number back to its component red, green, and
    blue values!! (If there IS one, please someone tell me what it is!)

    So, if there is no way to convert the single numbers back to rgb values OR
    to convert them to the new "color" values, then VB.NET has changed the whole
    system for assigning colors to controls, and is giving me no way to convert
    the numbers that were required before into the new system. That's not very
    nice!

    If anyone has an answer for this, please let me know.

    --
    Thanks.
    rohomes@hotmail .com
  • Mike McIntyre

    #2
    Re: How can you convert an integer into a VB.NET color?

    The table available through MSDN link beflow 'may' help you:




    "Bob Homes" <rohomes@hotmai l.com.(nospam)> wrote in message
    news:4BAA484A-708B-4E10-9944-20F8829BE6D6@mi crosoft.com...[color=blue]
    > In VB6, foreground and background colors of controls had to be assigned a
    > single number. If you knew the RGB values for the color, you still had to
    > convert them into the single number accepatable to the VB6 controls.
    >
    > In VB.NET, you can't set colors that way anymore, now you have to use a
    > "color". There is a way to convert RGB values to a "color", using the
    > Color.FromARGB method. But there doens't seem to be a way to convert the
    > old
    > single numbers into a "color".
    >
    > In VB6, there was a method to convert rgb values into a single number,
    > using
    > the RGB method. This method is still available in VB6 through the
    > Microsoft.Visua lBasic.Informat ion namespace. However, I have the single
    > numbers I used before (that were REQUIRED in VB6), I don't have separate
    > Red,
    > Green, and Blue values for those numbers. Neither VB6 nor VB.NET seem to
    > have
    > a method for converting a single number back to its component red, green,
    > and
    > blue values!! (If there IS one, please someone tell me what it is!)
    >
    > So, if there is no way to convert the single numbers back to rgb values OR
    > to convert them to the new "color" values, then VB.NET has changed the
    > whole
    > system for assigning colors to controls, and is giving me no way to
    > convert
    > the numbers that were required before into the new system. That's not very
    > nice!
    >
    > If anyone has an answer for this, please let me know.
    >
    > --
    > Thanks.
    > rohomes@hotmail .com[/color]


    Comment

    • GhostInAK

      #3
      Re: How can you convert an integer into a VB.NET color?

      Hello Bob,

      Check out the System.Drawing. ColorTranslator class. (hint: VB6 color storage
      is defined by OLE).

      -Boo
      [color=blue]
      > In VB6, foreground and background colors of controls had to be
      > assigned a single number. If you knew the RGB values for the color,
      > you still had to convert them into the single number accepatable to
      > the VB6 controls.
      >
      > In VB.NET, you can't set colors that way anymore, now you have to use
      > a "color". There is a way to convert RGB values to a "color", using
      > the Color.FromARGB method. But there doens't seem to be a way to
      > convert the old single numbers into a "color".
      >
      > In VB6, there was a method to convert rgb values into a single number,
      > using the RGB method. This method is still available in VB6 through
      > the Microsoft.Visua lBasic.Informat ion namespace. However, I have the
      > single numbers I used before (that were REQUIRED in VB6), I don't have
      > separate Red, Green, and Blue values for those numbers. Neither VB6
      > nor VB.NET seem to have a method for converting a single number back
      > to its component red, green, and blue values!! (If there IS one,
      > please someone tell me what it is!)
      >
      > So, if there is no way to convert the single numbers back to rgb
      > values OR to convert them to the new "color" values, then VB.NET has
      > changed the whole system for assigning colors to controls, and is
      > giving me no way to convert the numbers that were required before into
      > the new system. That's not very nice!
      >
      > If anyone has an answer for this, please let me know.
      >[/color]


      Comment

      • Bob Homes

        #4
        Re: How can you convert an integer into a VB.NET color?

        Mike, no, it really doesn't. I have a whole bunch of "strange" colors, not
        just the usual 7 or 8. Thanks anyway.

        rohomes@hotmail .com


        "Mike McIntyre" wrote:
        [color=blue]
        > The table available through MSDN link beflow 'may' help you:
        >
        > http://msdn.microsoft.com/library/de...alBasicNET.asp
        >
        >
        > "Bob Homes" <rohomes@hotmai l.com.(nospam)> wrote in message
        > news:4BAA484A-708B-4E10-9944-20F8829BE6D6@mi crosoft.com...[color=green]
        > > In VB6, foreground and background colors of controls had to be assigned a
        > > single number. If you knew the RGB values for the color, you still had to
        > > convert them into the single number accepatable to the VB6 controls.
        > >
        > > In VB.NET, you can't set colors that way anymore, now you have to use a
        > > "color". There is a way to convert RGB values to a "color", using the
        > > Color.FromARGB method. But there doens't seem to be a way to convert the
        > > old
        > > single numbers into a "color".
        > >
        > > In VB6, there was a method to convert rgb values into a single number,
        > > using
        > > the RGB method. This method is still available in VB6 through the
        > > Microsoft.Visua lBasic.Informat ion namespace. However, I have the single
        > > numbers I used before (that were REQUIRED in VB6), I don't have separate
        > > Red,
        > > Green, and Blue values for those numbers. Neither VB6 nor VB.NET seem to
        > > have
        > > a method for converting a single number back to its component red, green,
        > > and
        > > blue values!! (If there IS one, please someone tell me what it is!)
        > >
        > > So, if there is no way to convert the single numbers back to rgb values OR
        > > to convert them to the new "color" values, then VB.NET has changed the
        > > whole
        > > system for assigning colors to controls, and is giving me no way to
        > > convert
        > > the numbers that were required before into the new system. That's not very
        > > nice!
        > >
        > > If anyone has an answer for this, please let me know.
        > >
        > > --
        > > Thanks.
        > > rohomes@hotmail .com[/color]
        >
        >
        >[/color]

        Comment

        • gene kelley

          #5
          Re: How can you convert an integer into a VB.NET color?

          On Tue, 6 Jun 2006 03:58:48 +0000 (UTC), GhostInAK
          <ghostinak@gmai l.com> wrote:
          [color=blue]
          >Hello Bob,
          >
          >Check out the System.Drawing. ColorTranslator class. (hint: VB6 color storage
          >is defined by OLE).
          >
          >-Boo[/color]

          There's even a discussion in the Help file about VB6/VB2005 color
          handling changes. What a novel idea.

          Gene
          [color=blue]
          >[color=green]
          >> In VB6, foreground and background colors of controls had to be
          >> assigned a single number. If you knew the RGB values for the color,
          >> you still had to convert them into the single number accepatable to
          >> the VB6 controls.
          >>
          >> In VB.NET, you can't set colors that way anymore, now you have to use
          >> a "color". There is a way to convert RGB values to a "color", using
          >> the Color.FromARGB method. But there doens't seem to be a way to
          >> convert the old single numbers into a "color".
          >>
          >> In VB6, there was a method to convert rgb values into a single number,
          >> using the RGB method. This method is still available in VB6 through
          >> the Microsoft.Visua lBasic.Informat ion namespace. However, I have the
          >> single numbers I used before (that were REQUIRED in VB6), I don't have
          >> separate Red, Green, and Blue values for those numbers. Neither VB6
          >> nor VB.NET seem to have a method for converting a single number back
          >> to its component red, green, and blue values!! (If there IS one,
          >> please someone tell me what it is!)
          >>
          >> So, if there is no way to convert the single numbers back to rgb
          >> values OR to convert them to the new "color" values, then VB.NET has
          >> changed the whole system for assigning colors to controls, and is
          >> giving me no way to convert the numbers that were required before into
          >> the new system. That's not very nice!
          >>
          >> If anyone has an answer for this, please let me know.
          >>[/color]
          >[/color]

          Comment

          • hoppy

            #6
            Re: How can you convert an integer into a VB.NET color?

            =?Utf-8?B?Qm9iIEhvbWV z?= <rohomes@hotmai l.com.(nospam)> wrote in
            news:4BAA484A-708B-4E10-9944-20F8829BE6D6@mi crosoft.com:
            [color=blue]
            > In VB6, foreground and background colors of controls had to be
            > assigned a single number. If you knew the RGB values for the color,
            > you still had to convert them into the single number accepatable to
            > the VB6 controls.
            >
            > In VB.NET, you can't set colors that way anymore, now you have to use
            > a "color". There is a way to convert RGB values to a "color", using
            > the Color.FromARGB method. But there doens't seem to be a way to
            > convert the old single numbers into a "color".
            >
            > In VB6, there was a method to convert rgb values into a single number,
            > using the RGB method. This method is still available in VB6 through
            > the Microsoft.Visua lBasic.Informat ion namespace. However, I have the
            > single numbers I used before (that were REQUIRED in VB6), I don't have
            > separate Red, Green, and Blue values for those numbers. Neither VB6
            > nor VB.NET seem to have a method for converting a single number back
            > to its component red, green, and blue values!! (If there IS one,
            > please someone tell me what it is!)
            >
            > So, if there is no way to convert the single numbers back to rgb
            > values OR to convert them to the new "color" values, then VB.NET has
            > changed the whole system for assigning colors to controls, and is
            > giving me no way to convert the numbers that were required before into
            > the new system. That's not very nice!
            >
            > If anyone has an answer for this, please let me know.
            >[/color]

            Color.FromARGB( integer)

            It is overloaded. See the msdn help for color.fromArgb.

            to get the R, G and B from a color just use

            dim r as byte = myColor.R
            etc.

            Colors are just int32s. ARGB = byte order.
            So you can split with bitwise operators:

            here's some general messing about:

            Dim c As Color = Color.Chocolate
            Debug.WriteLine (Hex(c.ToArgb))
            ' outputs: FFD2691E
            Dim red As Byte = c.R
            Dim green As Byte = c.G
            Dim blue As Byte = c.B
            Dim c2 As Color = Color.FromArgb( &HFFD2691E)
            Debug.WriteLine (Hex(c2.ToArgb) )
            ' outputs: FFD2691E
            Dim red2 As Byte = &HD2
            Dim green2 As Byte = &H69
            Dim blue2 As Byte = &H1E
            Dim c3 As Color = Color.FromArgb( red2, green2, blue2)
            Debug.WriteLine (Hex(c3.ToArgb) )
            ' outputs: FFD2691E
            ' do it manually:
            Dim color4int As Integer = &HFFD2691E
            Dim R4 As Byte = (color4int >> 16) And &HFF
            Dim G4 As Byte = (color4int >> 8) And &HFF
            Dim b4 As Byte = (color4int And &HFF)
            Debug.WriteLine (String.Concat( "red: ", Hex(R4), ", green : ", Hex(G4), ",
            blue: ", Hex(b4)))
            ' outputs: red: D2, green : 69, blue: 1E
            ' reassemble:
            Dim color5int As Integer = (CInt(R4) << 16) Or (CInt(G4) << 8) Or b4
            Dim color5 As Color = Color.FromArgb( color5int)
            Debug.WriteLine (Hex(color5.ToA rgb))
            ' outputs D2691E - no alpha!
            ' use another override to supply the alpha for a base color:
            Dim color6 As Color = Color.FromArgb( &HFF, Color.FromArgb( color5int))
            Debug.WriteLine (Hex(color6.ToA rgb))
            ' outputs FFD2691E
            ' or you could have added it bitwise:
            Dim color7int As Integer = (&HFF << 24) Or (CInt(R4) << 16) Or (CInt(G4)
            << 8) Or b4
            Dim color7 As Color = Color.FromArgb( color7int)
            Debug.WriteLine (Hex(color6.ToA rgb))


            Always check the overload list for a method. It pops up in intellisense:

            after typing this:
            dim c as color = color.fromARGB(

            intellisense pops up
            "1 of 4 FromARGB(argb as integer) as System.Drawing. Color
            argb = A value specifying the 32 bit color value"

            and you can press the up and down arrows to see the other overloads.

            For more info on an overload, look in the help...

            Comment

            Working...