Optical Illusion

Anything Really. Just keep it clean!

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3332
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Optical Illusion

Post by JRL » Mon Jun 29, 2020 10:16 pm

Saw a version of this on VOX last week.
https://www.vox.com/science-and-health/ ... larization

I didn't invent this but I've had the code for a while.

Click on the little box to move it around. Click on the big box to get a new gradient.

The color of the little box never changes as you move it. Only when you pick a new gradient.


Code: Select all

OnEvent>Key_Down,VK27,0,Quit

SRT>Quit
  LibFunc>user32,ReleaseDC,RDCres1,HDC1_1,HDC1
  LibFunc>user32,ReleaseDC,RDCres1,HDC2_1,HDC2
  LibFunc>GDI32,DeleteObject,DOres,LIres
  Exit>0
END>Quit

GoSub>srtTitleBarSizeCalculation
CreateDir>%Temp_dir%Gradients

Dialog>Dialog1
object Dialog1: TForm
  Cursor = crHourGlass
  AlphaBlend = True
  AlphaBlendValue = 0
  BorderStyle = bsNone
  Caption = 'Gradient Mirror'
end
EndDialog>Dialog1

Dialog>Dialog2
object Dialog2: TForm
  Caption = 'Gradient Sample'
  ClientHeight = 600
  ClientWidth = 800
  Position = poScreenCenter
end
EndDialog>Dialog2

AddDialogHandler>Dialog1,,OnClose,Quit
AddDialogHandler>Dialog2,,OnClose,Quit
AddDialogHandler>Dialog2,,OnClick,Process
AddDialogHandler>Dialog2,,OnReSize,SizeReport


Dialog>Dialog3
object Dialog3: TForm
  BorderStyle = bsNone
  Caption = 'OneColorBox'
  ClientWidth = 100
  ClientHeight = 100
  Position = poScreenCenter
end
EndDialog>Dialog3

Let>ClickFlag=0

  AddDialogHandler>Dialog3,,OnMouseDown,MoveImage(1)
  AddDialogHandler>Dialog3,,OnMouseUp,StopImage


GetScreenRes>ScrX,ScrY
Let>PicCount=0
  LibFunc>user32,GetDC,HDC1,%Dialog1.handle%
  LibFunc>user32,GetDC,HDC2,%Dialog2.handle%
  LibFunc>Gdi32,SetStretchBltMode,SSBMres,HDC2,4
  LibFunc>Gdi32,SetBrushOrgEx,SBOEres,HDC2,0,0,



  
  Let>WIN_USEHANDLE=1
    MoveWindow>Dialog1.handle,0,0
    MoveWindow>Dialog2.handle,0,0
    WindowAction>1,Dialog1.handle
  Let>WIN_USEHANDLE=0

Let>vDotQty=5

Show>Dialog1
Show>dialog2
Show>dialog3

  Let>WIN_USEHANDLE=1
    GetWindowPos>Dialog3.handle,D3X,D3Y
    Sub>D3Y,200
    Movewindow>Dialog3.handle,D3X,D3Y
  Let>WIN_USEHANDLE=0

GoSub>Process
Label>IdleLoop
  Wait>0.01
  If>ClickFlag>1
    Add>ClickFlag,1
    If>ClickFlag>10
      Let>ClickFlag=0
    EndIf
  EndIf
Goto>IdleLoop

SRT>Process
  SetDialogProperty>Dialog2,,Cursor,10
  Add>PicCount,1
  
  //Set Color1 (Top) and Color2 (Bottom) to explicit colors
  //to create an explicit gradient.
  Random>16777216,Color1
  Random>16777216,Color2
  
  //Let>Color2=0
 // Let>Color1=16777215

    GoSub>Gradient

  SetDialogProperty>Dialog2,,Caption,Gradient Sample - %color1% to %Color2%
  GetDialogProperty>Dialog2,,ClientWidth,WinX
  GetDialogProperty>Dialog2,,ClientHeight,WinY
  LibFunc>Gdi32,StretchBlt,SBres,HDC2,0,0,WinX,WinY,HDC1,0,0,ScrX,ScrY,13369376
  Let>WIN_USEHANDLE=1
    GetWindowPos>Dialog2.handle,PosX,PosY
  Let>WIN_USEHANDLE=0
    Add>posX,%offsetX%
    Add>posY,%offsetY%
    Add>posX,1
    Add>posY,1
    Add>WinX,%PosX%
    Add>WinY,%PosY%
    Sub>WinX,1
    Sub>WinY,1
  //ScreenCapture>PosX,PosY,WinX,WinY,%Temp_dir%Gradients\GRad_%Color1%_to_%color2%_%PicCount%.png
  

//**BREAKPOINT**
  
  SetDialogProperty>Dialog2,,Cursor,0
  Let>Win_UseHandle=1
    GetWindowSize>dialog2.handle,GSw,GSh
    GetWindowPos>dialog2.handle,GSx,GSy
    SetFocus>dialog3.handle
    Let>Win_UseHandle=0
  Let>PixelX={round(%GSx%+(%GSw%/2))}
  Let>PixelY={round(%GSy%+(%GSh%/2))}
  GetPixelColor>PixelX,PixelY,CenterColor
  SetDialogProperty>Dialog3,,Color,CenterColor
  SetDialogProperty>Dialog3,,FormStyle,StayOnTop
END>Process

SRT>Gradient
  GetDialogProperty>Dialog1,,ClientHeight,high
  ColorToRGB>Color1,red1,grn1,blu1
  ColorToRGB>Color2,red2,grn2,blu2
  Let>redRatio={(%red2%-%red1%)/%high%}
  Let>grnRatio={(%grn2%-%grn1%)/%high%}
  Let>bluRatio={(%blu2%-%blu1%)/%high%}
  Let>gradk=0
  Repeat>Gradk
    Add>gradk,1
    Let>red1={%red1%+%redratio%}
    Let>grn1={%grn1%+%grnratio%}
    Let>blu1={%blu1%+%bluratio%}
    Let>newred={round(%red1%)}
    Let>newgrn={round(%grn1%)}
    Let>newblu={round(%blu1%)}
    RGB>%newred%,%newgrn%,%newblu%,newcolor
    Let>Ypos=%gradk%
    GoSub>DrawLine,Dialog1.handle,1,newcolor,1,Ypos,13000,Ypos
  Until>Gradk=high
END>Gradient

SRT>SizeReport
  Let>WIN_USEHANDLE=1
    GetWindowSize>Dialog2.handle,Width2,Height2
    SetDialogProperty>Dialog2,,Caption,(%Width2%%comma%%Height2%)
  Let>WIN_USEHANDLE=0
END>SizeReport

SRT>srtTitleBarSizeCalculation
Dialog>TitleBarSizeCalculation
Button=1,0,0,0,0,0
EndDialog>TitleBarSizeCalculation
Let>WIN_USEHANDLE=1
MoveWindow>TitleBarSizeCalculation.handle,0,0
GetWindowPos>TitleBarSizeCalculation.msButton1.handle,OffSetX,OffSetY
Let>WIN_USEHANDLE=0
END>srtTitleBarSizeCalculation




//DrawLine Usage:
//GoSub>DrawLine,WindowHandle,PenSize,PenColor,XStart,YStart,XEnd,YEnd

SRT>DrawLine
  LibFunc>user32,GetDC,HDC,%DrawLine_var_1%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawLine_var_2%,%DrawLine_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  Libfunc>gdi32,MoveToEx,mtres,HDC,%DrawLine_var_4%,%DrawLine_var_5%,0
  LibFunc>gdi32,LineTo,ltres,hdc,%DrawLine_var_6%,%DrawLine_var_7%
  LibFunc>gdi32,DeleteObject,DOres,Penres
  LibFunc>user32,ReleaseDC,RDCres,HDC_1,HDC
END>DrawLine


SRT>MoveImage
  Label>Loop
  If>ClickFlag=2
    Goto>LoopEnd
  EndIf
    Let>win_usehandle=1
    If>ClickFlag=0
      GetCursorPos>CurX,CurY
      GetWindowPos>Dialog3.handle,Xpos,Ypos
      Let>Xdiff={%curX%-%Xpos%}
      Let>Ydiff={%curY%-%Ypos%}
      Let>ClickFlag=1
    EndIf
      If>ClickFlag=1
        GetCursorPos>CurX,CurY
        Let>Xpos=%CurX%-%Xdiff%
        Let>Ypos=%CurY%-%Ydiff%
        MoveWindow>Dialog3.handle,Xpos,Ypos
        
      EndIf
    EndIf
    Let>win_usehandle=0
  Goto>Loop
  Label>LoopEnd
END>MoveImage

SRT>StopImage
  Let>ClickFlag=2
END>StopImage

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts