How to resolve the algorithm Greyscale bars/Display step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greyscale bars/Display step by step in the Lua programming language

Table of Contents

Problem Statement

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 through 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greyscale bars/Display step by step in the Lua programming language

Source code in the lua programming language

local nw = require("nw")
local app = nw:app()
local cw, ch = 320, 240
local win = app:window(cw, ch, "Grayscale Bars", false)
function win:repaint()
  local cr = win:bitmap():cairo()
  local ystride = ch/4
  for y = 0, 3 do
    local i, n = 1, 2^(y+3)
    local xstride = cw/n
    for x = 0, n-1 do
      cr:rectangle(x*xstride, y*ystride, xstride, ystride)
      local gray = x / (n-1)
      if y%2>0 then gray=1-gray end
      cr:rgb(gray, gray, gray)
      cr:fill()
    end
  end
end
win:show()
app:run()


  

You may also check:How to resolve the algorithm Monty Hall problem step by step in the Perl programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Stata programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the Wren programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Joy programming language