Greyscale bars/Display: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 9:
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">package
{
import flash.display.Sprite;
Line 34:
}
}
}</langsyntaxhighlight>
 
=={{header|Ada}}==
{{libheader|GTK}}
{{libheader|GtkAda}}
<langsyntaxhighlight Adalang="ada">with Gtk.Window; use Gtk.Window;
with Gtk.Enums;
with Gtk.Handlers;
Line 124:
Gtk.Main.Main;
end Greyscale;
</syntaxhighlight>
</lang>
 
=={{header|Amazing Hopper}}==
{{trans|AWK}}
Version: hopper-FLOW!
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <flow.h>
#include <flow-term.h>
Line 164:
NEXT
RET
</syntaxhighlight>
</lang>
 
=={{header|ANSI Standard BASIC}}==
Line 170:
{{trans|BBC BASIC}}
 
<langsyntaxhighlight ANSIlang="ansi Standardstandard BASICbasic">100 SET WINDOW 0,1279,0,1023
110 REM (0,0) is the bottom left of the display
120 SET AREA COLOR 1 ! Select color one for drawing
Line 184:
220 NEXT b
230 NEXT row
240 END</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Requires the GDI+ Standard Library by tic: http://www.autohotkey.com/forum/viewtopic.php?t=32238
<langsyntaxhighlight AHKlang="ahk">h := A_ScreenHeight
w := A_ScreenWidth
pToken := gdip_Startup()
Line 243:
QColor(r, g, b){
return 0xFF000000 | (r << 16) | (g << 8) | (b)
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">
BEGIN {
nrcolors = 8
Line 272:
}
}
</syntaxhighlight>
</lang>
 
=={{header|BASIC256}}==
<syntaxhighlight lang="text">
h=ceil(graphheight/4)
for row=1 to 4
Line 286:
next n
next row
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic">MODE 8:REM 640 x 512 pixel display mode: BBC BASIC gives 2 graphics points per pixel
REM (0,0) is the bottom left of the display
GCOL 1 :REM Select colour one for drawing
Line 302:
RECTANGLE FILL w%*b%,py%,w%,256
NEXT b%
NEXT row%</langsyntaxhighlight>
 
=={{header|C}}==
{{libheader|GTK}}
<langsyntaxhighlight lang="c">#include <gtk/gtk.h>
/* do some greyscale plotting */
void gsplot (cairo_t *cr,int x,int y,double s) {
Line 340:
gtk_main ();
return 0;
} </langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{libheader|System.Windows.Forms}}
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Windows.Forms;
Line 376:
return colorBars;
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
Line 383:
file greytones.h
</PRE>
<langsyntaxhighlight lang="cpp">#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
Line 396:
void paintEvent( QPaintEvent * ) ;
} ;
#endif</langsyntaxhighlight>
<pre>file greytones.cpp</pre>
<langsyntaxhighlight lang="cpp">#include <QtGui>
#include "greytones.h"
 
Line 431:
run++ ;
}
}</langsyntaxhighlight>
<PRE>
file main.cpp
</PRE>
<langsyntaxhighlight lang="cpp">#include <QApplication>
#include "greytones.h"
 
Line 444:
window.show( ) ;
return app.exec( ) ;
}</langsyntaxhighlight>
 
=={{header|Component Pascal}}==
Line 451:
[[File:GreyScaleCp.png|thumb|right]]
 
<langsyntaxhighlight lang="oberon2">
MODULE RosettaGreys;
IMPORT Views, Ports, Properties, Controllers, StdLog;
Line 528:
 
"RosettaGreys.Deposit; StdCmds.Open"
</syntaxhighlight>
</lang>
 
=={{header|EasyLang}}==
Line 534:
[https://easylang.online/apps/_greyscale.html Run it]
 
<syntaxhighlight lang="text">n = 8
for row range 4
sz = 100 / n
Line 547:
.
n *= 2
.</langsyntaxhighlight>
 
=={{header|Eiffel}}==
Line 555:
[https://github.com/ljr1981/rosettacode_answers/blob/main/testing/rc_greyscale_bars/eifgreyscale.png PNG Output]
 
<langsyntaxhighlight lang="eiffel">
feature -- Test routines
 
Line 627:
down: INTEGER = -1 --| From `white' to `black' or ...
up: INTEGER = 1 --| From `black' to `white'
</syntaxhighlight>
</lang>
 
=={{header|Euler Math Toolbox}}==
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function grayscale(y1,y2,n,direction=1) ...
$ loop 0 to n-1;
Line 651:
$endfunction
>grayscales:
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 01-09-2017
' compile with: fbc -s console
' or compile with: fbc -s gui
Line 688:
 
Sleep
End</langsyntaxhighlight>
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
fakewidth =!= dummy
 
Line 713:
}
}
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
[[File:GrayscalesR.png|thumb|right]]
<langsyntaxhighlight lang="futurebasic">
_window = 1
 
Line 772:
 
HandleEvents
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 778:
</pre>
And here's another clever and shorter solution:
<langsyntaxhighlight lang="futurebasic">void local fn BuildWindow
CGRect r = {0,0,640,400}
long i, j, bars = 8
Line 802:
fn BuildWindow
 
HandleEvents</langsyntaxhighlight>
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Form_Open()
Dim iRow, iCol, iClr As Integer 'For Row, Column and Colour
Dim iInc As Integer = 4 'To calculate RGB colour
Line 833:
Next
 
End</langsyntaxhighlight>
 
'''[http://www.cogier.com/gambas/GreyScale.png Click here for image of the output]'''
Line 840:
{{libheader|Go Graphics}}
{{trans|Java}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 879:
greyBars(dc)
dc.SavePNG("greybars.png")
}</langsyntaxhighlight>
 
{{out}}
Line 889:
This program uses an inlined XPM file which is scaled to fill an entire GTK fullscreen window
 
<langsyntaxhighlight Haskelllang="haskell">import Graphics.UI.Gtk
import Graphics.UI.Gtk.Gdk.GC
import Control.Monad.Trans (liftIO)
Line 946:
"AADDFFHHJJLLNNPPRRTTVVXXZZbbddffhhjjllnnpprrttvvxxzz11336688..**",
"*+.9876543210zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCA"
]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 953:
 
[[File:Greyscale_unicon.png|thumb|right]]
<langsyntaxhighlight Iconlang="icon">link graphics,printf,numbers
procedure main()
Line 983:
put(TC.bands,band(height)) # bottom sentinal
return TC
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 992:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j"> load 'viewmat'
NB. size=. 2{.".wd'qm' NB. J6
NB. size=. getscreenwh_jgtk_ '' NB. J7
Line 1,000:
togreyscale=. (256#. [:<.255 255 255&*)"0
'rgb' viewmat (4<.@%~{:size)# (64<.@%~{.size)#"1 togreyscale bars
wd 'pmove 0 _30 ',":size [ wd 'psel ',1{::,hforms_jviewmat_'' NB. J9</langsyntaxhighlight>
 
Note that hardware changes over the years have left their mark on interface protocols, which shows up here as language changes.
Line 1,006:
=={{header|Java}}==
using basically the same code as in the C++ example
<langsyntaxhighlight Javalang="java">import javax.swing.* ;
import java.awt.* ;
 
Line 1,052:
Greybars gb = new Greybars( ) ;
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Live Demo: http://jsfiddle.net/gcN9g/embedded/result/
<langsyntaxhighlight JavaScriptlang="javascript"><html><body>
<script type="text/javascript">
var width = 640; var height = 400;
Line 1,085:
</script>
</body></html>
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight Julialang="julia">using Gtk, Cairo, ColorTypes
 
function generategrays(n, screenwidth)
Line 1,142:
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
wait(cond)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1
 
import java.awt.Color
Line 1,196:
fun main(args: Array<String>) {
GreyBars()
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
Black boxes were added around each color for ease of counting the boxes.
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
 
Line 1,233:
end
 
</syntaxhighlight>
</lang>
Resulting [http://www.diga.me.uk/greyscale.gif GreyScale image] without the outlines.
 
Line 1,239:
{{libheader|nw}}
{{libheader|cairo}}
<langsyntaxhighlight lang="lua">local nw = require("nw")
local app = nw:app()
local cw, ch = 320, 240
Line 1,259:
end
win:show()
app:run()</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<langsyntaxhighlight lang="mathematica">CreateDocument[ Graphics[ Flatten@Table[
{ If[EvenQ[#3], GrayLevel[ 1. - j/#1 ], GrayLevel[ j/#1 ]],
Rectangle[{j #2, 7*#3}, {#2 (j + 1), (#3 + 1) 7}]}, {j, 0, #1}] & @@@
{{7, 8, 3}, {15, 4, 2}, {31, 2, 1}, {63, 1, 0} }
,ImageSize -> Full], WindowFrame -> "Frameless", WindowSize -> Full]</langsyntaxhighlight>
[[File:greyscales.jpg|thumb|right]]
 
Line 1,273:
 
3ds max provides customizable maps like gradient to the user, but you can also write it:
<syntaxhighlight lang="maxscript">
<lang MAXScript>
fn drawBarRow _bmp _row _width _number _inverse=
(
Line 1,343:
b = bitmap_verticalBars()
display b
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{libheader|gintro}}
<langsyntaxhighlight Nimlang="nim">import gintro/[glib, gobject, gtk, gio, cairo]
 
const
Line 1,413:
let app = newApplication(Application, "Rosetta.GreyscaleBars")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">open Graphics
 
let round x = truncate (floor (x +. 0.5))
Line 1,438:
done
) bars;
ignore(read_key())</langsyntaxhighlight>
 
Run with:
Line 1,444:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub partition {
my($all, $div) = @_;
my @marks = 0;
Line 1,480:
bars($h2,$w,4,1),
bars($h3,$w,5,0),
bars($h4,$w,6,1);</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/Greyscale-bars-perl5.png See Greyscale-bars-perl5] (offsite image)
 
Line 1,488:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Greyscale_bars.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Greyscale_bars.exw
Line 1,551:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let Pgm # Create PGM of 384 x 288 pixels
(make
(for N 4
Line 1,569:
(prinl (length (car Pgm)) " " (length Pgm))
(prinl 255)
(for Y Pgm (apply wr Y)) ) )</langsyntaxhighlight>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Clear the screen.
Line 1,616:
If the flag is set, put "right-to-left" into the gradient's direction.
If the flag is not set, put "left-to-right" into the gradient's direction.
Draw the gradient.</langsyntaxhighlight>
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">
//Aamrun, 3rd July 2022
 
Line 1,641:
drawPanel(255,0,64,3*height/4);
}
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
 
Macro Check(Function)
Line 1,706:
CloseScreen()
EndIf
End</langsyntaxhighlight>
Press Enter or Escape to exit the programs's display.
 
=={{header|Python}}==
{{libheader|livewires}}
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/env python
#four gray scaled stripes 8:16:32:64 in Python 2.7.1
 
Line 1,736:
while keys_pressed() != [' ']: # press spacebar to close window
pass
</syntaxhighlight>
</lang>
 
=={{header|R}}==
Create a 4x64 matrix representing the described pattern, set margins to 0 so the image will fill the display, and plot the matrix in grayscale using the "image" function:
[[File:GrayscalesR.png|thumb|right]]
<syntaxhighlight lang="r">
<lang R>
mat <- matrix(c(rep(1:8, each = 8) / 8,
rep(16:1, each = 4) / 16,
Line 1,749:
par(mar = rep(0, 4))
image(t(mat[4:1, ]), col = gray(1:64/64), axes = FALSE)
</syntaxhighlight>
</lang>
 
Or, this can be generalized with the function below, which produces the pattern for an arbitrary number of rows (though rows become visibly indistinguishable after about row 5):
[[File:GrayscalesR-6.png|thumb|right]]
<syntaxhighlight lang="r">
<lang R>
grayscalesImage <- function(nrow = 4) {
X <- matrix(NA, nrow = nrow, ncol = 2^(nrow + 2))
Line 1,765:
## Example ##
grayscalesImage(6) # produces image shown in screenshot to the right
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 1,773:
[[File:Grayscale-pict.png|thumb|right]]
 
<langsyntaxhighlight lang="racket">
#lang racket/gui
(require slideshow/pict)
Line 1,794:
(vc-append (grays 1/8 'right) (grays 1/16 'left)
(grays 1/32 'right) (grays 1/64 'left))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my ($width,$height) = 1280,768;
my $PGM = open "Greyscale-bars-perl6.pgm", :w orelse die "Can't create Greyscale-bars-perl6.pgm: $_";
Line 1,832:
my @marks = ((1/$div,2/$div ... 1) X* $all)».round;
@marks Z- 0,|@marks;
}</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/Greyscale-bars-perl6.png See Greyscale-bars-perl6] (offsite image)
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
<lang vb>
Declare Sub PaintCanvas
 
Line 1,865:
 
Form.showmodal
</syntaxhighlight>
</lang>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Greyscale bars/Display
 
Line 1,927:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>
Output:
https://www.dropbox.com/s/01iywg04iwubf55/GreyscaleBars.jpg?dl=0
 
=={{header|Run BASIC}}==
<langsyntaxhighlight Runbasiclang="runbasic">for i = 1 to 4
incr = int(256 / (i * 8))
c = 256
Line 1,943:
next i
html "</table>"
end</langsyntaxhighlight>
<pre>Run in a browser</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.swing._
 
class GreyscaleBars extends Component {
Line 1,965:
}
}
}</langsyntaxhighlight>
Open window:
[[File:greyscalebars_scala.png|thumb|right]]
<langsyntaxhighlight lang="scala">new MainFrame(){
title="Greyscale bars"
visible=true
preferredSize=new Dimension(640, 320)
contents=new GreyscaleBars()
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "draw.s7i";
include "keybd.s7i";
Line 2,006:
end for;
ignore(getc(KEYBOARD));
end func;</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk 8.5
Line 2,030:
}
incr y $dy
}</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 2,084:
}
 
var Game = GreyBars.new(640, 320)</langsyntaxhighlight>
 
=={{header|XPL0}}==
Floating point is used to get the full range of black to white.
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int Q, N, W, B, C, Y;
[SetVid($112); \640x480x24 graphics
Line 2,104:
Q:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">open window 1024, 600
w = peek("winwidth")
h = peek("winheight")
Line 2,129:
pause .1
next n
next row</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 2,135:
{{trans|XPL0}}
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">img:=PPM(640,480);
foreach q in ([0..3]){ //quarter of screen
n:=(8).shiftLeft(q); //number of bars
Line 2,147:
}
}
img.write(File("foo.ppm","wb"));</langsyntaxhighlight>
{{out}}
Same as the R image (but smaller):
Line 2,157:
and we have a set of 8 bars:
 
<langsyntaxhighlight lang="zxbasic">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
25 BORDER 0: CLS
Line 2,165:
60 NEXT c
70 REM at this point the cursor has wrapped, so we don't need a newline
80 NEXT r</langsyntaxhighlight>
 
{{omit from|GUISS}}
10,339

edits