Canvas: identifying mouse over color ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 200
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Canvas: identifying mouse over color ?

Post by Otrebor »

Code: Select all

OpenWindow(0,0,0,200,400,"Canvas test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
CanvasGadget(1,0,0,200,310)
Global FONTE1=LoadFont(1,"Courier New",9)
Procedure Draw_text()
  
  StartDrawing(CanvasOutput(1))
  DrawingFont(FONTE1)
  FrontColor($000000)
  BackColor($FFFFFF)
  For f = 0 To 10
    DrawText(0,line,"not a link"):DrawText(140,line,"link",RGB(255,0,0))    
    line+15
  Next
  StopDrawing()
  
EndProcedure
Draw_text()

Repeat
  Event=WaitWindowEvent()
  
  If EventType()=#PB_EventType_MouseMove
    y=(GetGadgetAttribute(1, #PB_Canvas_MouseY))
    x=(GetGadgetAttribute(1, #PB_Canvas_MouseX))
    StartDrawing(CanvasOutput(1))
    z=Point(x,y) 
    StopDrawing()
    If z = 255
      SetGadgetAttribute(1,#PB_Canvas_Cursor, #PB_Cursor_Hand)
      Debug "over a link"
    Else
     SetGadgetAttribute(1,#PB_Canvas_Cursor, #PB_Cursor_Default)
    EndIf
 EndIf
  
Until Event = #PB_Event_CloseWindow 
How to correctly identify the cursor over red color?
Thanks.

Edit: My example is not 100% correct. The red link can appear everywhere.
User avatar
STARGÅTE
Addict
Addict
Posts: 2084
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Canvas: identifying mouse over color ?

Post by STARGÅTE »

Due to Anti-aliasing and ClearType drawing of the text only some parts of the final image have pure red color. Most of the pixels near the red text have a mixture of white and red. So how should such idea work?
Why you want to use the color as indicator instead of a frame box?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 200
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: Canvas: identifying mouse over color ?

Post by Otrebor »

Well, i did lot of experiments (this is part of my debugger)!
In my current version i use a lot of Textgadget (6 per line), with #SS_NOTIFY and resizing according needed! Obviously this not practical and is slow. But this works!

Now i'm trying something faster. I found the canvas very fast and simple.
I improved a little the detection with this modification:

Code: Select all

OpenWindow(0,0,0,200,400,"Canvas test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
CanvasGadget(1,0,0,200,310)
Global FONTE1=LoadFont(1,"Courier New",9)
Procedure Draw_text()
  
  StartDrawing(CanvasOutput(1))
  DrawingFont(FONTE1)
  FrontColor($000000)
  BackColor($FFFFFF)
  For f = 0 To 10
    DrawText(0,line,"not a link"):DrawText(140,line,"link",RGB(255,0,0),RGB(252,252,252))    
    line+15
  Next
  StopDrawing()
  
EndProcedure
Draw_text()

Repeat
  Event=WaitWindowEvent()
  
  If EventType()=#PB_EventType_MouseMove
    y=(GetGadgetAttribute(1, #PB_Canvas_MouseY))
    x=(GetGadgetAttribute(1, #PB_Canvas_MouseX))
    StartDrawing(CanvasOutput(1))
    z1=Point(x,y) & 255:z2=Point(x+1,y) & 255
    StopDrawing()
    If  z1+z2<510 And z1+z2>499
      SetGadgetAttribute(1,#PB_Canvas_Cursor, #PB_Cursor_Hand)
      Debug "over a link"
    Else
     SetGadgetAttribute(1,#PB_Canvas_Cursor, #PB_Cursor_Default)
    EndIf
 EndIf
  
Until Event = #PB_Event_CloseWindow 

By frame box, what you mean?
Thank you
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Canvas: identifying mouse over color ?

Post by infratec »

Unfortunately the CanvasGadget does not include an alpha channel.
So you need to modify the background.
Else you could set the alpha value to 254 for the DrawText() of the link.
Then the detection would be much easier.

Code: Select all

OpenWindow(0,0,0,200,400,"Canvas test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
CanvasGadget(1,0,0,200,310)
Global FONTE1=LoadFont(1,"Courier New",9)
Procedure Draw_text()
  
  StartDrawing(CanvasOutput(1))
  DrawingFont(FONTE1)
  FrontColor(RGB(0,0,0))
  BackColor(RGB(255,255,255))
  For f = 0 To 10
    DrawText(0,line,"not a link")
    DrawText(140, line, "link", RGB(255, 0, 0), RGB(255,255,254))
    line+20
  Next
  StopDrawing()
  
EndProcedure
Draw_text()

Repeat
  Event=WaitWindowEvent()
  
  If EventType()=#PB_EventType_MouseMove
    y=(GetGadgetAttribute(1, #PB_Canvas_MouseY))
    x=(GetGadgetAttribute(1, #PB_Canvas_MouseX))
    StartDrawing(CanvasOutput(1))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    z = 0
    
    For ix = x - 1 To x + 1
      For iy = y - 1 To y + 1
        If ix >= 0 And ix < GadgetWidth(1) And iy >= 0 And iy < GadgetHeight(1)
          c = Point(ix, iy) 
          If c = RGB(255,255,254) Or c = RGB(255, 0, 0)
            z + 1
          EndIf
        EndIf
      Next iy
    Next ix
    
    
    
    StopDrawing()
    If z > 0
      SetGadgetAttribute(1,#PB_Canvas_Cursor, #PB_Cursor_Hand)
      Debug "over a link"
    Else
      SetGadgetAttribute(1,#PB_Canvas_Cursor, #PB_Cursor_Default)
    EndIf
  EndIf
  
Until Event = #PB_Event_CloseWindow 
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 200
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: Canvas: identifying mouse over color ?

Post by Otrebor »

@infratec
Your code seems more precise than mine.
Thank you!
Post Reply