Render the shade unicode box characters as per the spec
This commit is contained in:
parent
b6baa99fce
commit
a01dad8c3c
@ -3,7 +3,6 @@
|
|||||||
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import os
|
|
||||||
from functools import partial as p
|
from functools import partial as p
|
||||||
from itertools import repeat
|
from itertools import repeat
|
||||||
|
|
||||||
@ -377,16 +376,42 @@ def hblock(buf, width, height, frac=1, gravity='left'):
|
|||||||
buf[c] = 255
|
buf[c] = 255
|
||||||
|
|
||||||
|
|
||||||
def shade(buf, width, height, frac=1/4):
|
def shade(buf, width, height, light=False, invert=False):
|
||||||
rand = bytearray(os.urandom(width * height))
|
square_sz = max(1, width // 12)
|
||||||
cutoff = int(frac * 255)
|
number_of_rows = height // square_sz
|
||||||
|
number_of_cols = width // square_sz
|
||||||
|
nums = tuple(range(square_sz))
|
||||||
|
|
||||||
for r in range(height):
|
dest = bytearray(width * height) if invert else buf
|
||||||
off = width * r
|
|
||||||
for c in range(width):
|
for r in range(number_of_rows):
|
||||||
q = off + c
|
y = r * square_sz
|
||||||
if rand[q] < cutoff:
|
is_odd = r % 2 != 0
|
||||||
buf[q] = 255
|
if is_odd:
|
||||||
|
continue
|
||||||
|
fill_even = r % 4 == 0
|
||||||
|
for yr in nums:
|
||||||
|
y = r * square_sz + yr
|
||||||
|
if y >= height:
|
||||||
|
break
|
||||||
|
off = width * y
|
||||||
|
for c in range(number_of_cols):
|
||||||
|
if light:
|
||||||
|
fill = (c % 4) == (0 if fill_even else 2)
|
||||||
|
else:
|
||||||
|
fill = (c % 2 == 0) == fill_even
|
||||||
|
if fill:
|
||||||
|
for xc in nums:
|
||||||
|
x = (c * square_sz) + xc
|
||||||
|
if x >= width:
|
||||||
|
break
|
||||||
|
dest[off + x] = 255
|
||||||
|
if invert:
|
||||||
|
for y in range(height):
|
||||||
|
off = width * y
|
||||||
|
for x in range(width):
|
||||||
|
q = off + x
|
||||||
|
buf[q] = 255 - dest[q]
|
||||||
|
|
||||||
|
|
||||||
def quad(buf, width, height, x=0, y=0):
|
def quad(buf, width, height, x=0, y=0):
|
||||||
@ -465,9 +490,9 @@ box_chars = {
|
|||||||
'▎': [p(hblock, frac=1/4)],
|
'▎': [p(hblock, frac=1/4)],
|
||||||
'▏': [p(hblock, frac=1/8)],
|
'▏': [p(hblock, frac=1/8)],
|
||||||
'▐': [p(hblock, frac=1/2, gravity='right')],
|
'▐': [p(hblock, frac=1/2, gravity='right')],
|
||||||
'░': [p(shade, frac=1/4)],
|
'░': [p(shade, light=True)],
|
||||||
'▒': [p(shade, frac=2/4)],
|
'▒': [shade],
|
||||||
'▓': [p(shade, frac=3/4)],
|
'▓': [p(shade, invert=True)],
|
||||||
'▔': [p(vblock, frac=1/8)],
|
'▔': [p(vblock, frac=1/8)],
|
||||||
'▕': [p(hblock, frac=1/8, gravity='right')],
|
'▕': [p(hblock, frac=1/8, gravity='right')],
|
||||||
'▖': [p(quad, y=1)],
|
'▖': [p(quad, y=1)],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user