Greyscale bars/Display: Difference between revisions

From Rosetta Code
Content added Content deleted
(promote to task)
Line 27: Line 27:


[[Category:Test card]]
[[Category:Test card]]
=={{header|PureBasic}}==
<lang PureBasic>If Not InitKeyboard(): End: EndIf ;can't init keyboard
If Not InitSprite(): End: EndIf ;can't init sprite/screen library
If Not ExamineDesktops(): End: EndIf ;can't retrieve information about desktop

Define height.f, width.f, depth
height.f = DesktopHeight(0)
width.f = DesktopWidth(0)
depth = DesktopDepth(0)

If OpenScreen(width, height, depth, "Press ENTER to exit")
Define vsCount, v, h, columns, columnWidth, endColor, shade
StartDrawing(ScreenOutput())
vsCount = 4
For v = 0 To 3
columns = (v + 1) * 8
columnWidth = Round(width / columns, #PB_Round_Up)
endColor = $FFFFFF * (v % 2) ;alternate between black and white for first and last bar
Box(0, (height * v) / vsCount, columnWidth, height / vsCount, endColor)

For h = 1 To columns - 2
If v % 2 = 0
shade = 256 / columns * (h + 1)
Else
shade = 256 / columns * (columns - (h + 1))
EndIf
Box((width * h) / columns, (height * v) / vsCount, columnWidth, height / vsCount, RGB(shade, shade, shade))
Next
Box((width * (columns - 1)) / columns, (height * v) / vsCount, columnWidth, height / vsCount, $FFFFFF - endColor)
Next
StopDrawing()
FlipBuffers()

Repeat
Delay(10)
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Return)
CloseScreen()
EndIf</lang>
Press Enter or Escape to exit the programs's display.

Revision as of 18:41, 6 June 2011

Task
Greyscale bars/Display
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to display a series of vertical greyscale bars (contrast bars) across the width of the display.

For the top quarter of the display, the left hand bar should be black, and we then incrementally step though six shades of grey until we have a white bar on the right hand side of the display. (This gives a total of 8 bars)

For the second quarter down, we start with white and step down through 14 shades of gray, getting darker until we have black on the right hand side of the display. (This gives a total of 16 bars).

Halfway down the display, we start with black, and produce 32 bars, ending in white, and for the last quarter, we start with white and step through 62 shades of grey, before finally arriving at black in the bottom right hand corner, producing a total of 64 bars for the bottom quarter.

ZX Spectrum Basic

ZX Spectrum Basic cannot natively produce greyscale. However, the colours have been cleverly arranged, so that the native colours give monochrome signals in sequential order of brightness. Wind the colour down, or use a black and white television and we have a set of 8 bars:

<lang basic> 10 REM wind the colour down or use a black and white television to see greyscale bars 20 REM The ZX Spectrum display is 32 columns wide, so we have 8 columns of 4 spaces 30 FOR r=0 TO 20: REM There are 21 rows 40 FOR c=0 TO 7: REM We use the native colour sequence here 50 PRINT " ";: REM four spaces, the semicolon prevents newline 60 NEXT c 70 REM at this point the cursor has wrapped, so we don't need a newline 80 NEXT r </lang>

PureBasic

<lang PureBasic>If Not InitKeyboard(): End: EndIf ;can't init keyboard If Not InitSprite(): End: EndIf ;can't init sprite/screen library If Not ExamineDesktops(): End: EndIf ;can't retrieve information about desktop

Define height.f, width.f, depth height.f = DesktopHeight(0) width.f = DesktopWidth(0) depth = DesktopDepth(0)

If OpenScreen(width, height, depth, "Press ENTER to exit")

 Define vsCount, v, h, columns, columnWidth, endColor, shade
 StartDrawing(ScreenOutput())
   vsCount = 4
   For v = 0 To 3
     columns = (v + 1) * 8
     columnWidth = Round(width / columns, #PB_Round_Up)
     endColor = $FFFFFF * (v % 2)     ;alternate between black and white for first and last bar
     Box(0, (height * v) / vsCount, columnWidth, height / vsCount, endColor)
     For h = 1 To columns - 2
       If v % 2 = 0
         shade = 256 / columns * (h + 1)
       Else
         shade = 256 / columns * (columns - (h + 1))
       EndIf 
       Box((width * h) / columns, (height * v) / vsCount, columnWidth, height / vsCount, RGB(shade, shade, shade))
     Next
     
     Box((width * (columns - 1)) / columns, (height * v) / vsCount, columnWidth, height / vsCount, $FFFFFF - endColor)
   Next 
 StopDrawing()
 FlipBuffers()
 Repeat
   Delay(10)
   ExamineKeyboard()
 Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Return)
 CloseScreen()

EndIf</lang> Press Enter or Escape to exit the programs's display.