none
use GetPixel() with IF construct RRS feed

  • Question

  • hello,
    I have a question.

    I want to graphicsWindow.GetPixel with If. See below,


    GraphicsWindow.BrushColor = "Yellow"
    GraphicsWindow.FillRectangle(0,0,100,100)

    If(GraphicsWindow.GetPixel(50,50) = "Yellow") Then
    GraphicsWindow.ShowMessage("Yellow!","")
    EndIf

     

    But,This Program dosen't show "Yellow!". So, must write below.


    If(GraphicsWindow.GetPixel(50,50) = "#FFFF00") Then
    GraphicsWindow.ShowMessage("Yellow!","")
    EndIf

     

    I think that you are more happy if can write colorname in If statement.
    And, Children can understand more more easyly.

    --

    sorry , I'm a Japanease student.
    because I cannot understand so difference English and my English may be worng.

    • Changed type litdevModerator Sunday, August 8, 2010 10:25 AM This is a question
    Sunday, August 8, 2010 8:19 AM

Answers

  • The GetPixel returns a string with the colour in hex format "#FFFF00".  It is the strings that are comapred in the If statement.  I guess this is because a colour can be anything from "#000000" to "#FFFFFF", whereas the named colours are only a subset of the possible colours.  I think that is just how it is - it would be possible to write an extension to convert from named colours to their hex equivalent.

    I have an extisting extension that does something like this at:

    http://cid-92f7df0e57232aba.office.live.com/self.aspx/.Public/SmallBasic/C%5E3%20Extensions/Colours%5E_v0.9.zip

    To use the extension, close SmallBasic, then copy the contents of the zip file above to the intallation directory C:\Program Files\Microsoft\Small Basic\lib .  You may need to create the lib directory if it doesn't exist, then restart SmallBasic and the example below should work.  Anyone else using your code will also require the extension dll.

    GraphicsWindow.BrushColor = "Yellow" ' OR GraphicsWindow.BrushColor = Colour.Yellow
    GraphicsWindow.FillRectangle(0,0,100,100)

    If (GraphicsWindow.GetPixel(50,50) = Colour.Yellow) Then
      GraphicsWindow.ShowMessage("Yellow!","")
    EndIf

    • Marked as answer by umisama Wednesday, August 11, 2010 2:57 PM
    Sunday, August 8, 2010 10:24 AM
    Moderator
  •  In lieu of an extension, you could also assign the hex value data to an array or variables

    color["yellow"] = "#FFFF00"
    yellow = "#FFFF00"
    
    GraphicsWindow.BrushColor = "Yellow"
    GraphicsWindow.FillRectangle(0,0,100,100)
    
    If (GraphicsWindow.GetPixel(50,50) = color["Yellow"]) Then
     GraphicsWindow.ShowMessage("Yellow!","by array")
    EndIf
    If (GraphicsWindow.GetPixel(50,50) = Yellow) Then
     GraphicsWindow.ShowMessage("Yellow!","by variable name")
    EndIf
    
    

     

    • Marked as answer by umisama Wednesday, August 11, 2010 2:57 PM
    Sunday, August 8, 2010 11:11 AM

All replies

  • The GetPixel returns a string with the colour in hex format "#FFFF00".  It is the strings that are comapred in the If statement.  I guess this is because a colour can be anything from "#000000" to "#FFFFFF", whereas the named colours are only a subset of the possible colours.  I think that is just how it is - it would be possible to write an extension to convert from named colours to their hex equivalent.

    I have an extisting extension that does something like this at:

    http://cid-92f7df0e57232aba.office.live.com/self.aspx/.Public/SmallBasic/C%5E3%20Extensions/Colours%5E_v0.9.zip

    To use the extension, close SmallBasic, then copy the contents of the zip file above to the intallation directory C:\Program Files\Microsoft\Small Basic\lib .  You may need to create the lib directory if it doesn't exist, then restart SmallBasic and the example below should work.  Anyone else using your code will also require the extension dll.

    GraphicsWindow.BrushColor = "Yellow" ' OR GraphicsWindow.BrushColor = Colour.Yellow
    GraphicsWindow.FillRectangle(0,0,100,100)

    If (GraphicsWindow.GetPixel(50,50) = Colour.Yellow) Then
      GraphicsWindow.ShowMessage("Yellow!","")
    EndIf

    • Marked as answer by umisama Wednesday, August 11, 2010 2:57 PM
    Sunday, August 8, 2010 10:24 AM
    Moderator
  •  In lieu of an extension, you could also assign the hex value data to an array or variables

    color["yellow"] = "#FFFF00"
    yellow = "#FFFF00"
    
    GraphicsWindow.BrushColor = "Yellow"
    GraphicsWindow.FillRectangle(0,0,100,100)
    
    If (GraphicsWindow.GetPixel(50,50) = color["Yellow"]) Then
     GraphicsWindow.ShowMessage("Yellow!","by array")
    EndIf
    If (GraphicsWindow.GetPixel(50,50) = Yellow) Then
     GraphicsWindow.ShowMessage("Yellow!","by variable name")
    EndIf
    
    

     

    • Marked as answer by umisama Wednesday, August 11, 2010 2:57 PM
    Sunday, August 8, 2010 11:11 AM
  • In program GHL987 getPixel() returns "#000000" for the circle but #FFFFFF for the text (click anywhere).

    Why does this happen and is there a way to fix this?

     

    Friday, July 1, 2011 7:50 PM
  • It looks like the background colour isn't recorded with the GetPixel.  This looks like a quirk (bug perhaps) of SmallBasic.  Suggest you add it to the things to fix for v1.0 thread.

    A workaround for the background colour is to draw a full screen filled rectangle.

    The text is ok, its just that you clear it (fill a rectangle with white) before getting the colour.

    Slightly modified test, which I think works as expected, apart from the BG colour, fixed with a workaround.  Import FQB899.

     


    Friday, July 1, 2011 8:15 PM
    Moderator
  • Thank you for you respons and workaround.

    I found an earlier message about the behaviour of getPixel in the 1.0-thread, so I won't post a second reply with the same bug.

     

    I wrote a simple fill-area procedure that needs getpixel(), see KML266

    Friday, July 1, 2011 9:09 PM
  • I like the organic growth of the fill, here's another method using a stack to store last filled pixels which is the start for looking for any adjacent unfilled pixels : see WTC719.
    Friday, July 1, 2011 9:17 PM
    Moderator