Log In  

Cart #simple_dither-1 | 2024-03-30 | Embed ▽ | License: CC4-BY-NC-SA
3

thought i'd post a demonstration of generating simple dither patterns for fillp in case it was useful to anyone!

feel free to use under the CC license or mit license or public domain

dither.lua:

-- threshold map from https://en.wikipedia.org/wiki/Ordered_dithering
local threshold_map = {
   0,  8,  2, 10,
  12,  4, 14,  6,
   3, 11,  1,  9,
  15,  7, 13,  5,
}

local function make_mask (value)
  local mask = 0
  for i = 1, 16 do
    mask = mask * 2
    if threshold_map [i] >= value then
      mask = mask + 1
    end
  end
  return mask
end

local dither_masks = {}
for i = 0, 16 do
  dither_masks[i] = make_mask (i)
end

-- value 0-1
function dither_mask (value)
  return dither_masks [mid (0, math.floor (value * 16 + 0.5), 16)]
end

main.lua:

include 'dither.lua'

local W, H = 480, 270

local color1 = 9
local color2 = 30

function _draw ()
  for i = 0, H-1 do
    fillp (dither_mask (i/H))
    rectfill (0, i, W, i, (color1 << 8) | color2)
  end
  fillp ()
end
P#145139 2024-03-30 16:39 ( Edited 2024-04-01 13:17)


[Please log in to post a comment]