Greyscale bars/Display: Difference between revisions

From Rosetta Code
Content added Content deleted
m ({{omit from|GUISS}})
Line 76: Line 76:
return app.exec( ) ;
return app.exec( ) ;
}</lang>
}</lang>

=={{header|Icon}} and {{header|Unicon}}==
This procedure uses code from the [[Colour_bars/Display|Colour bars/Display task]], specifically the: ''DrawTestCard'' procedure and ''testcard'', ''band'', and ''bar''
records.

<lang Icon>link graphics,printf,numbers
procedure main()
DrawTestCard(GreyScale_TestCard())
WDone()
end

procedure greyscale(l,h,s) #: generate s greys over range l:h
every i := round(l to h+1 by ((h-l)/(s-1.))) do
suspend sprintf("%d,%d,%d",i,i,i) # return rgb black-grey-white
end

procedure GreyScale_TestCard() #: return greyscale testcard
TC := testcard(,"GreyScale Test Card",
width := 800, height := 600,
list(numbands := 4) )
maxv := 2^16-1 # largest colour value
every (iv := [], i := 1 to numbands) do { # for each band
every put(v := [], greyscale(0,maxv,2^(2+i))) # compute greyscale
put(iv, if i%2 = 0 then v else reverse(v)) # switch directions
}

every r := height/numbands * ((i := 1 to numbands)-1) + 1 do {
TC.bands[i] := band(r,[])
every c := width/(*iv[i]) * ((j := 1 to *iv[i])-1) + 1 do
put(TC.bands[i].bars, bar( c, iv[i,j]))
put((TC.bands[i]).bars, bar(width)) # right sentinal
}
put(TC.bands,band(height)) # bottom sentinal
return TC
end</lang>

{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/graphics.icn graphics.icn supports graphics]
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides sprintf, etc.]
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers.icn provides round]


=={{header|J}}==
=={{header|J}}==

Revision as of 03:41, 15 July 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) with a sufficient number of bars to span the entire 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.

C++

using Qt 4.6

file greytones.h

<lang C++>

  1. ifndef MYWIDGET_H
  2. define MYWIDGET_H
  3. include <QWidget>

class QPaintEvent ;

class MyWidget : public QWidget { public :

  MyWidget( ) ;

protected :

  void paintEvent( QPaintEvent * ) ;

} ;

  1. endif</lang>
file greytones.cpp

<lang C++>#include <QtGui>

  1. include "greytones.h"

MyWidget::MyWidget( ) {

  setGeometry( 0, 0 , 640 , 480 ) ;

}

void MyWidget::paintEvent ( QPaintEvent * ) {

  QBrush myBrush( Qt::SolidPattern ) ;
  QPainter myPaint( this ) ;
  int run = 0 ; //how often have we run through the loop ?
  int colorcomp = 0 ;
  for ( int columncount = 8 ; columncount < 128 ; columncount *= 2 ) {
     int colorgap = 255 / columncount ;
     int columnwidth = 640 / columncount ; // 640 being the window width
     int columnheight = 480 / 4 ; //we're looking at quarters
     if ( run % 2 == 0 ) { //we start with black columns 

colorcomp = 0 ;

     }
     else { //we start with white columns 

colorcomp = 255 ; colorgap *= -1 ; //we keep subtracting color values

     }
     int ystart = 0 + columnheight * run ; //determines the y coordinate of the first column per row
     int xstart = 0 ;
     for ( int i = 0 ; i < columncount ; i++ ) {

myBrush.setColor( QColor( colorcomp, colorcomp , colorcomp ) ) ; myPaint.fillRect( xstart , ystart , columnwidth , columnheight , myBrush ) ; xstart += columnwidth ; colorcomp += colorgap ; //we choose the next color

     }
     run++ ;
  }

}</lang>

file main.cpp

<lang C++>#include <QApplication>

  1. include "greytones.h"

int main( int argc, char * argv[ ] ) {

  QApplication app( argc , argv ) ;
  MyWidget window ;
  window.setWindowTitle( QApplication::translate( "greyScales" , "grey scales demonstration" ) ) ;
  window.show( ) ;
  return app.exec( ) ;

}</lang>

Icon and Unicon

This procedure uses code from the Colour bars/Display task, specifically the: DrawTestCard procedure and testcard, band, and bar records.

<lang Icon>link graphics,printf,numbers

procedure main()

  DrawTestCard(GreyScale_TestCard())
  WDone()

end

procedure greyscale(l,h,s) #: generate s greys over range l:h every i := round(l to h+1 by ((h-l)/(s-1.))) do suspend sprintf("%d,%d,%d",i,i,i) # return rgb black-grey-white end

procedure GreyScale_TestCard() #: return greyscale testcard

  TC := testcard(,"GreyScale Test Card",
                 width := 800, height := 600, 
                 list(numbands := 4) )
  maxv := 2^16-1                                      # largest colour value
  every (iv := [], i := 1 to numbands) do {           # for each band       
     every put(v := [], greyscale(0,maxv,2^(2+i)))    # compute greyscale

put(iv, if i%2 = 0 then v else reverse(v)) # switch directions }

  every r := height/numbands * ((i := 1 to numbands)-1) + 1 do {
     TC.bands[i] := band(r,[])
     every c := width/(*iv[i]) * ((j := 1 to *iv[i])-1) + 1 do 
        put(TC.bands[i].bars, bar( c, iv[i,j]))
     put((TC.bands[i]).bars, bar(width))              # right sentinal 
     }
  put(TC.bands,band(height))                          # bottom sentinal
  return TC

end</lang>

graphics.icn supports graphics printf.icn provides sprintf, etc. numbers.icn provides round

J

Solution: <lang j> load 'viewmat'

  size=. 2{.".wd'qm' NB. J6
  size=. getscreenwh_jgtk_  NB. J7
  rows=. (2^3+i.4),._1^i.4
  bars=. ((64%{.)#[:(<:@|%~i.)*/)"1 rows
  togreyscale=. (256#. [:<.255 255 255&*)"0
  'rgb' viewmat (4<.@%~{:size)# (64<.@%~{.size)#"1 togreyscale bars</lang>

Note that this solution is not posted directly to the screen but to a viewmat window, which may not be centered.

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.

Tcl

Library: Tk

<lang tcl>package require Tcl 8.5 package require Tk 8.5

wm attributes . -fullscreen 1 pack [canvas .c -highlightthick 0] -fill both -expand 1

  1. Add more values into this to do more greyscale bar variations

set splits {8 16 32 64} set dy [expr {[winfo screenheight .c] / [llength $splits]}] set y 0 foreach s $splits {

   set dx [expr {double([winfo screenwidth .c]) / $s}]
   set dc [expr {double(0xFF) / ($s-1)}]
   for {set i 0} {$i < $s} {incr i} {

set c [expr {int($i * $dc)}] set x [expr {int($i * $dx)}] .c create rectangle $x $y [expr {$x+$dx+1}] [expr {$y+$dy+1}] \

           -fill [format "#%02x%02x%02x" $c $c $c] -outline {}
   }
   incr y $dy

}</lang>

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>