Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Extracting colors

Extracting colors

Scheduled Pinned Locked Moved Visual Basic
tutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pasan148
    wrote on last edited by
    #1

    I want to extract Red, Green and Blue values separately from value which return from Color.ToArgb function. How to do that.

    L 2 Replies Last reply
    0
    • P Pasan148

      I want to extract Red, Green and Blue values separately from value which return from Color.ToArgb function. How to do that.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Assuming VB.NET, the Color struct holds all that is required. ToArgb() is the method that turns a Color into an Int32, that is quite the opposite of what you want. Have a look at the FromArgb(Int32) method and the R, G, B properties. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      P 1 Reply Last reply
      0
      • L Luc Pattyn

        Assuming VB.NET, the Color struct holds all that is required. ToArgb() is the method that turns a Color into an Int32, that is quite the opposite of what you want. Have a look at the FromArgb(Int32) method and the R, G, B properties. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        P Offline
        P Offline
        Pasan148
        wrote on last edited by
        #3

        I think you don't undetstand what i meant. Consider following code.

        Dim img As New Bitmap(PictureBox1.Image)
        Dim col As Integer = d.GetPixel(x, y).ToArgb ' x and y means any x and y coordination of Bitmap

        Variable col will hold some value that return from GetPixel function. Now I want to extract R value, G value and B value separately from variable col. That's what i want.

        L L 2 Replies Last reply
        0
        • P Pasan148

          I think you don't undetstand what i meant. Consider following code.

          Dim img As New Bitmap(PictureBox1.Image)
          Dim col As Integer = d.GetPixel(x, y).ToArgb ' x and y means any x and y coordination of Bitmap

          Variable col will hold some value that return from GetPixel function. Now I want to extract R value, G value and B value separately from variable col. That's what i want.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          See the Color[^] methods for extraction of individual parts.

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          1 Reply Last reply
          0
          • P Pasan148

            I think you don't undetstand what i meant. Consider following code.

            Dim img As New Bitmap(PictureBox1.Image)
            Dim col As Integer = d.GetPixel(x, y).ToArgb ' x and y means any x and y coordination of Bitmap

            Variable col will hold some value that return from GetPixel function. Now I want to extract R value, G value and B value separately from variable col. That's what i want.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            At the risk of repeating myself: Dim Red As Integer = Color.FromArgb(col).R :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            D P 2 Replies Last reply
            0
            • L Luc Pattyn

              At the risk of repeating myself: Dim Red As Integer = Color.FromArgb(col).R :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              Just kidding. :)

              L 1 Reply Last reply
              0
              • P Pasan148

                I want to extract Red, Green and Blue values separately from value which return from Color.ToArgb function. How to do that.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                The mathematical alternative is:

                Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                    showColorComponents(Color.LightYellow)
                    showColorComponents(Color.FromArgb(11, 22, 33, 44))
                End Sub
                Private Sub showColorComponents(ByVal col As Color)
                    log("col=" & col.ToString & " = " & col.A & "," & col.R & "," & col.G & "," & col.B)
                    Dim I As Int32 = col.ToArgb()
                    log("I=0x" & I.ToString("X8"))
                    Dim B As Int32 = I And 255
                    Dim G As Int32 = (I >> 8) And 255
                    Dim R As Int32 = (I >> 16) And 255
                    Dim A As Int32 = (I >> 24) And 255
                    log("ARGB=" & A & "," & R & "," & G & "," & B)
                End Sub
                

                :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                1 Reply Last reply
                0
                • D dan sh

                  Just kidding. :)

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  To be absolutely sure, I also gave an alternative that does not rely on the features of the Color type. See below message. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    At the risk of repeating myself: Dim Red As Integer = Color.FromArgb(col).R :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    P Offline
                    P Offline
                    Pasan148
                    wrote on last edited by
                    #9

                    Thank you so much. Now i can understand it

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups