RosettaCodeData/Task/Colour-bars-Display/Nim/colour-bars-display.nim

42 lines
998 B
Nim
Raw Permalink Normal View History

2025-02-27 18:35:13 -05:00
import cairo
2023-07-01 11:58:00 -04:00
const
2025-02-27 18:35:13 -05:00
Width = 600
Height = 400
2023-07-01 11:58:00 -04:00
2025-02-27 18:35:13 -05:00
proc drawBars(surface: ptr Surface) =
2023-07-01 11:58:00 -04:00
## Draw the color bars.
2025-02-27 18:35:13 -05:00
const Colors = [(0.0, 0.0, 0.0), # Black.
(1.0, 0.0, 0.0), # Red.
(0.0, 1.0, 0.0), # Green.
(0.0, 0.0, 1.0), # Blue.
(1.0, 0.0, 1.0), # Magenta.
(0.0, 1.0, 1.0), # Cyan.
(1.0, 1.0, 0.0), # Yellow.
(1.0, 1.0, 1.0) # White.
]
2023-07-01 11:58:00 -04:00
const
RectWidth = float(Width div Colors.len)
RectHeight = float(Height)
2025-02-27 18:35:13 -05:00
let context = create(surface)
2023-07-01 11:58:00 -04:00
var x = 0.0
2025-02-27 18:35:13 -05:00
for (r, g, b) in Colors:
2023-07-01 11:58:00 -04:00
context.rectangle(x, 0, RectWidth, RectHeight)
2025-02-27 18:35:13 -05:00
context.setSourceRgb(r, g, b)
2023-07-01 11:58:00 -04:00
context.fill()
x += RectWidth
2025-02-27 18:35:13 -05:00
context.destroy()
2023-07-01 11:58:00 -04:00
2025-02-27 18:35:13 -05:00
let surface = imageSurfaceCreate(FormatRgb24, 600, 400)
surface.drawBars()
if surface.writeToPng("color_bars.png") != StatusSuccess:
quit "Error while writing file.", QuitFailure
surface.destroy()