Results 1 to 3 of 3

Thread: Color shades rgb help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Color shades rgb help

    hello i recently got a response from you guys on how to, Search for a pixel and replace it well now im stuck on finding out how i could do shades, So im using this code
    Code:
       Dim newImage As Bitmap = New Bitmap(PictureBox1.BackgroundImage)
            Dim bmp As Bitmap = DirectCast(PictureBox1.BackgroundImage, Bitmap)
            For x As Integer = 0 To bmp.Width - 1
                For y As Integer = 0 To bmp.Height - 1
                    If bmp.GetPixel(x, y) = (Color.FromArgb(0, 0, 0)) Then
                        newImage.SetPixel(x, y, Color.White)
                        PictureBox2.BackgroundImage = newImage
                    Else
                    End If
                Next
            Next
    now how could i have it. say if x,y= any shade of black Then.'Take Action'
    im making my own paint type tool so im jw how i could paint all black even if it is just 1 digit off from 0,0,0'<- Black' or could someone give me some example code on how to loop threw some lines of r,g,b values so i could use that in my function

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Color shades rgb help

    Hi Tweaker, Here's a typical way to check if two colors match within a certain tolerance. I think that's what you are after.

    You check if the Red, Green and Blue components are each within tolerance limits. For example:
    Code:
    Dim tolerance as Integer = 8
    Dim testColor as Color = Color.Black
    
    'then put this in the middle of the loop instead of just If bmp.GetPixel(x, y) = ...
    
    Dim clr As Color = bmp.GetPixel(x, y)
    If Math.Abs(clr.R - testColor.R) < tolerance AndAlso _
       Math.Abs(clr.G - testColor.G) < tolerance AndAlso _ 
       Math.Abs(clr.B - testColor.B) < tolerance Then _
       newImage.SetPixel(x, y, Color.White)
    End If
    You can obviously try this out with different colors and tolerances.

    As minitech pointed out in the other thread, using GetPixel and SetPixel isn't the most efficient way to do pixel processing. But I think it's a good idea to get it working this way first, and leave speeding it up to later if it turns out to be necessary. (By the way, you should put PictureBox2.BackgroundImage = ... outside the loop, because otherwise you will be doing it thousands of times when you really only need to do it once.)

    BB

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: Color shades rgb help

    Quote Originally Posted by boops boops View Post
    Hi Tweaker, Here's a typical way to check if two colors match within a certain tolerance. I think that's what you are after.

    You check if the Red, Green and Blue components are each within tolerance limits. For example:
    Code:
    Dim tolerance as Integer = 8
    Dim testColor as Color = Color.Black
    
    'then put this in the middle of the loop instead of just If bmp.GetPixel(x, y) = ...
    
    Dim clr As Color = bmp.GetPixel(x, y)
    If Math.Abs(clr.R - testColor.R) < tolerance AndAlso _
       Math.Abs(clr.G - testColor.G) < tolerance AndAlso _ 
       Math.Abs(clr.B - testColor.B) < tolerance Then _
       newImage.SetPixel(x, y, Color.White)
    End If
    You can obviously try this out with different colors and tolerances.

    As minitech pointed out in the other thread, using GetPixel and SetPixel isn't the most efficient way to do pixel processing. But I think it's a good idea to get it working this way first, and leave speeding it up to later if it turns out to be necessary. (By the way, you should put PictureBox2.BackgroundImage = ... outside the loop, because otherwise you will be doing it thousands of times when you really only need to do it once.)

    BB
    wow man this was 100% exactly what i was looking for
    Your amazing also pro Thanks So Much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width