Gray Scale

Share your advanced PureBasic knowledge/code with the community.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Gray Scale

Post by chris319 »

This handy program draws a gray scale for adjusting your monitor.

Code: Select all

InitSprite(): InitKeyboard()
#screenWd = 1365: #screenHt = 768
OpenWindow(0,0,0, #screenWd, #screenHt, #NULL$,#PB_Window_BorderLess)
AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape) ;"ESCAPE" KEY TO QUIT PROGRAM

StartDrawing(WindowOutput(0))
ShowCursor_(#False) ;HIDE MOUSE POINTER
Box(0,0,#SCREENWD,#SCREENHT,$000000)

#STEPS = 12
For ct = 0 To #STEPS - 1
  lum.f = (ct / #STEPS) * 255
  Box(ct * (#screenWd / #STEPS), 0, #screenWd / #STEPS, #screenHt, RGB(lum,lum,lum))
Next





















StopDrawing()

Repeat
event = WaitWindowEvent()
  
  If event = #PB_Event_Repaint

  ElseIf event = #PB_Event_Menu ;KEYBOARD INPUT
      menuItem = EventMenu()

      Select menuItem

Case #PB_Shortcut_Escape
;          FreeFont(1): FreeFont(2)
;          If ImageID(0) <> 0: FreeImage(0): CloseWindow(0): End: EndIf ;ESCAPE KEY PRESSED
          End
                    
    EndSelect 
    
EndIf
ForEver
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: Gray Scale

Post by RichardL »

Hi,
A version that works with multiple screens and takes the R,G,B values all the way to 255.
RichardL

Code: Select all


NumDeskTops = ExamineDesktops()
For n = 0 To NumDeskTops-1
  ScreenWd = DesktopWidth(n) : Debug "Width "+Str(ScreenWd)
  ScreenHt = DesktopHeight(n)
  OpenWindow(n,DesktopX(n),0, ScreenWd, ScreenHt, #Null$,#PB_Window_BorderLess)
  AddKeyboardShortcut(n, #PB_Shortcut_Escape, #PB_Shortcut_Escape) ;"ESCAPE" KEY TO QUIT PROGRAM
  StartDrawing(WindowOutput(n))
    Box(0,0,ScreenWd,ScreenHt,$000000)
    Steps = 16
    X = 0
    Lum.f = 0
    LumStep.f = 255 / (Steps-1)
    XStep = ScreenWd/Steps
    For ct = 1 To Steps
      Debug Str(X)+"  "+Str(Lum)
      Box(X,0,XStep,ScreenHt, RGB(Lum,Lum,Lum))
      Lum + LumStep
      X   + XStep
    Next
  StopDrawing()
Next

Repeat
  event = WaitWindowEvent()
  If event = #PB_Event_Repaint
    
  ElseIf event = #PB_Event_Menu ;KEYBOARD INPUT
    menuItem = EventMenu()
    
    Select menuItem
        
      Case #PB_Shortcut_Escape
        Break
        
    EndSelect 
    
  EndIf
ForEver
End
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Gray Scale

Post by IdeasVacuum »

That's good code RichardL - highlighted just how different my monitors are, even though they are the same make and model. Not sure how I'm going to adjust them though........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Gray Scale

Post by luis »

IdeasVacuum wrote:Not sure how I'm going to adjust them though........
Windows 7 and higher have a built in calibration software for gamma and .. in some way... color.
http://www.pcmag.com/article2/0,2817,2414252,00.asp

For gamma (the only thing you can really set quite reliably by just using your eyes and the environment light you are normally exposed) I use this software with good results -> http://quickgamma.de/indexen.html

For color... better buy a cheap hw + sw combination like http://spyder.datacolor.com/display-calibration/ if possible.
"Have you tried turning it off and on again ?"
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Gray Scale

Post by chris319 »

This version continues the white bar all the way to the edge of the screen, and repaints if the window was covered (such as closing the debug window).

Code: Select all

Global screenWd, screenHt

Procedure DrawGrayScale()
      StartDrawing(WindowOutput(n))
        Box(0,0,ScreenWd,ScreenHt,$ffffff)
        Steps = 16
        X = 0
        Lum.f = 0
        LumStep.f = 255 / (Steps-1)
        XStep = ScreenWd/Steps
        For ct = 1 To Steps
          Debug Str(X)+"  "+Str(Lum)
          Box(X,0,XStep,ScreenHt, RGB(Lum,Lum,Lum))
          Lum + LumStep
          X   + XStep
        Next
        StopDrawing()
        Debug "Press ESC to exit."
EndProcedure  

    NumDeskTops = ExamineDesktops()
    For n = 0 To NumDeskTops-1
      ScreenWd = DesktopWidth(n) : Debug "Width "+Str(ScreenWd)
      ScreenHt = DesktopHeight(n)
      OpenWindow(n,DesktopX(n),0, ScreenWd, ScreenHt, #NULL$,#PB_Window_BorderLess)
      AddKeyboardShortcut(n, #PB_Shortcut_Escape, #PB_Shortcut_Escape) ;"ESCAPE" KEY TO QUIT PROGRAM
      
DrawGrayScale()
Next

    Repeat
      event = WaitWindowEvent()
      If event = #PB_Event_Repaint
        DrawGrayScale()
        
      ElseIf event = #PB_Event_Menu ;KEYBOARD INPUT
        menuItem = EventMenu()
       
        Select menuItem
           
          Case #PB_Shortcut_Escape
            Break
           
        EndSelect
       
      EndIf
    ForEver
    End
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: Gray Scale

Post by RichardL »

Hi,
Your procedure DrawGrayScale() needs to have the Window number passed to it...
and then the Repaint will need to provide a Window number (probably all of them!)
Richard
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Gray Scale

Post by chris319 »

It was intended to occupy the entire display while the user sets his brightness, contrast, etc., not to draw a gray scale in each open window.

Code: Select all

Global screenWd, screenHt

Procedure DrawGrayScale()
      StartDrawing(WindowOutput(0))
        Box(0,0,ScreenWd,ScreenHt,$ffffff)
        Steps = 16
        X = 0
        Lum.f = 0
        LumStep.f = 255 / (Steps-1)
        XStep = ScreenWd/Steps
        For ct = 1 To Steps
          Debug Str(X)+"  "+Str(Lum)
          Box(X,0,XStep,ScreenHt, RGB(Lum,Lum,Lum))
          Lum + LumStep
          X   + XStep
        Next
        Debug "Press ESC to exit"
        StopDrawing()
EndProcedure  

ExamineDesktops()
      ScreenWd = DesktopWidth(0) ;: Debug "Width "+Str(ScreenWd)
      ScreenHt = DesktopHeight(0)
      OpenWindow(0,0,0, ScreenWd,ScreenHt, #NULL$,#PB_Window_BorderLess)
      AddKeyboardShortcut(n, #PB_Shortcut_Escape, #PB_Shortcut_Escape) ;"ESCAPE" KEY TO QUIT PROGRAM
      DrawGrayScale()

    Repeat
      event = WaitWindowEvent()
      If event = #PB_Event_Repaint
        DrawGrayScale()
        
      ElseIf event = #PB_Event_Menu ;KEYBOARD INPUT
        menuItem = EventMenu()
       
        Select menuItem
           
          Case #PB_Shortcut_Escape
            Break
           
        EndSelect
       
      EndIf
    ForEver
    End
    
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Gray Scale

Post by chris319 »

luis wrote:
IdeasVacuum wrote:Not sure how I'm going to adjust them though........
Windows 7 and higher have a built in calibration software for gamma and .. in some way... color.
http://www.pcmag.com/article2/0,2817,2414252,00.asp

For gamma (the only thing you can really set quite reliably by just using your eyes and the environment light you are normally exposed) I use this software with good results -> http://quickgamma.de/indexen.html

For color... better buy a cheap hw + sw combination like http://spyder.datacolor.com/display-calibration/ if possible.
That's fine with just one combination of computer and monitor, such as a laptop. With one computer and several monitors, IMO you're better off setting the computer's gamma to the standard of 2.2, which can be done with QuickGamma, and dealing with gamma issues in hardware for each individual monitor.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Gray Scale

Post by luis »

Good point, I use it with a single monitor per PC.
With multiple monitors connected to a single PC your approach it's probably better and easier to manage (for you and the software running on it).
"Have you tried turning it off and on again ?"
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Gray Scale

Post by IdeasVacuum »

Darn - Avast really really doesn't like QuickGamma V3 (For XP/Vista). Apparently it is a danger to the entire universe, maybe more. :)

Edit: QuickGamma is effective, but one of my monitors is determined to keep it's independence.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Gray Scale

Post by chris319 »

No problem here running Quick Gamma.

In this thread: http://www.purebasic.fr/english/viewtop ... 12&t=59993 I posted a toy program which will allow you to adjust the Windows gamma ramp. You can set it to 2.2 (the Windows de-facto standard) and unless your monitor is grossly off, it should be pretty close. I'm not a fan of these programs where you squint at a pattern on the screen and try to match gray levels. At least with my gamma toy or Quick Gamma you can set the gamma to the known value of 2.2 and adjust your monitor hardware while you squint at circles.

One gripe about the Windows gamma adjuster is that it is not calibrated, i.e. you don't know what value you're setting it to. If it becomes misadjusted you have to squint at dots all over again instead of returning it to a known, repeatable value.
Post Reply