Colour pinstripe/Printer: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 58: Line 58:
=={{header|Phix}}==
=={{header|Phix}}==
See the print_cb function of [[Colour_pinstripe/Display#Phix]].
See the print_cb function of [[Colour_pinstripe/Display#Phix]].

Note the function draw_to() draws 4 bands each 1/4 of the page height, however
cdCanvasGetSize() actually returns {width, height, width_mm, height_mm}, the
canvas size in pixels and in millimetres, and from that it is assumed it would
be trivial to change the calculation of h from height/4 to 1 inch, and also the
"for y=1 to 4 do" to "while height do".


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==

Revision as of 01:34, 20 July 2018

Task
Colour pinstripe/Printer
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to create 1 point wide colour vertical pinstripes with a sufficient number of pinstripes to span the entire width of the colour graphics printer. The pinstripes should alternate between each individual cartridge ink and ink pair and black and white pinstripes should be included. A typical pinstripe sequence woud be black, red, green, blue, magenta, cyan, yellow, white.

After the first inch of printing, we switch to a wider 2 pixel wide vertical pinstripe pattern. and to 3 point wide vertical for the next inch, and then 4 point wide, etc. This trend continues for the entire length of the page (or for 12 inches of run length in the case of a printer using continuous roll stationery). After printing the test pattern the page is ejected (or the test pattern is rolled clear of the printer enclosure, in the case of continuous roll printers).

Note that it is an acceptable solution to use the smallest marks that the language provides, rather than working at native printer resolution, where this is not achievable from within the language.

Optionally, on systems where the printer resolution cannot be determined, it is permissible to prompt the user for printer resolution, and to calculate point size based on user input, enabling fractional point sizes to be used.

BBC BASIC

This program first displays a Print Dialogue so the printer can be selected. <lang bbcbasic> PD_RETURNDC = 256

     _LOGPIXELSY = 90
     
     DIM pd{lStructSize%, hwndOwner%, hDevMode%, hDevNames%, \
     \      hdc%, flags%, nFromPage{l&,h&}, nToPage{l&,h&}, \
     \      nMinPage{l&,h&}, nMaxPage{l&,h&}, nCopies{l&,h&}, \
     \      hInstance%, lCustData%, lpfnPrintHook%, lpfnSetupHook%, \
     \      lpPrintTemplateName%, lpSetupTemplateName%, \
     \      hPrintTemplate%, hSetupTemplate%}
     pd.lStructSize% = DIM(pd{})
     pd.hwndOwner% = @hwnd%
     pd.flags% = PD_RETURNDC
     
     SYS "PrintDlg", pd{} TO ok%
     IF ok%=0 THEN QUIT
     SYS "DeleteDC", @prthdc%
     @prthdc% = pd.hdc%
     *MARGINS 0,0,0,0
     
     dx% = @vdu%!236-@vdu%!232
     dy% = @vdu%!244-@vdu%!240
     SYS "GetDeviceCaps", @prthdc%, _LOGPIXELSY TO dpi%
     
     DIM rc{l%,t%,r%,b%}
     DIM colour%(7)
     colour%() = &000000, &0000FF, &00FF00, &FF0000, \
     \           &FF00FF, &FFFF00, &00FFFF, &FFFFFF
     
     VDU 2,1,32,3
     pitch% = 1
     FOR y% = 0 TO dy% STEP dpi%
       col% = 0
       FOR x% = 0 TO dx%-pitch% STEP pitch%
         rc.l% = x% : rc.r% = x% + pitch%
         rc.t% = y% : rc.b% = y% + dpi%
         SYS "CreateSolidBrush", colour%(col% MOD 8) TO brush%
         SYS "FillRect", @prthdc%, rc{}, brush%
         SYS "DeleteObject", brush%
         col% += 1
       NEXT
       pitch% += 1
     NEXT y%
     VDU 2,1,12,3</lang>

Phix

See the print_cb function of Colour_pinstripe/Display#Phix.

Note the function draw_to() draws 4 bands each 1/4 of the page height, however cdCanvasGetSize() actually returns {width, height, width_mm, height_mm}, the canvas size in pixels and in millimetres, and from that it is assumed it would be trivial to change the calculation of h from height/4 to 1 inch, and also the "for y=1 to 4 do" to "while height do".

PicoLisp

<lang PicoLisp>(load "@lib/ps.l")

  1. Using circular lists for an endless supply of colors
  2. (black red green blue magenta cyan yellow white)

(setq

  Red   (0    100    0     0    100    0    100   100 .)
  Green (0     0    100    0     0    100   100   100 .)
  Blue  (0     0     0    100   100   100    0    100 .) )

(call 'lpr

  (pdf "pinstripes"
     (a4)  # 595 x 842 dots
     (let (I 0  Step 1)
        (for X 595
           (color (car Red) (car Green) (car Blue)
              (vline X 0 842) )
           (when (= Step (inc 'I))
              (zero I)
              (pop 'Red)
              (pop 'Green)
              (pop 'Blue) )
           (when (=0 (% X 72))  # 1 inch
              (zero I)
              (inc 'Step) ) ) )
     (page) ) )</lang>

Racket

The drawing code is exactly the same code as Colour_pinstripe/Display#Racket, only drawing onto a printer device context now.

<lang Racket>

  1. lang racket/gui

(define parts 4)

(define dc (new printer-dc%)) (send* dc (start-doc "Colour Pinstripe") (start-page))

(define-values [W H] (send dc get-size)) (define parts 4) (define colors

 '("Black" "Red" "Green" "Blue" "Magenta" "Cyan" "Yellow" "White"))

(send dc set-pen "black" 0 'transparent) (send dc set-brush "black" 'solid) (define H* (round (/ H parts))) (for ([row parts])

 (define Y (* row H*))
 (for ([X (in-range 0 W (add1 row))] [c (in-cycle colors)])
   (send dc set-brush c 'solid)
   (send dc draw-rectangle X Y (add1 row) H*)))

(send* dc (end-page) (end-doc)) </lang>

Tcl

This code assumes that the page's printable area is 8.5"×11".

Library: Tk

<lang tcl>package require Tk

  1. Allocate a temporary drawing surface

canvas .c

  1. The cycle of colors we want to use

set colors {black red green blue magenta cyan yellow white}

  1. Draw the output we want

for {set y 0;set dx 1} {$y < 11*72} {incr y 72;incr dx} {

   for {set x 0;set c 0} {$x < 8.5*72} {incr x $dx;incr c} {

.c create rectangle $x $y [expr {$x+$dx+1}] [expr {$y+73}] \ -fill [lindex $colors [expr {$c%[llength $colors]}]] -outline {}

   }

}

  1. Send postscript to default printer, scaled 1 pixel -> 1 point

exec lp - << [.c postscript -height $y -width $x -pageheight $y -pagewidth $x]

  1. Explicit exit; no GUI desired

exit</lang>