Calvin (Deutschbein)
W9Mon: 21 Oct
>>> hex(1)
'0x1'
>>> hex(10)
'0xa'
>>> hex(100)
'0x64'
>>> for i in range(0x8,0x12):
... print(str(i),hex(i))
...
8 0x8
9 0x9
10 0xa
11 0xb
12 0xc
13 0xd
14 0xe
15 0xf
16 0x10
17 0x11
from pgl import *
max = 400
colors = ['#FFFFFF','#FF0000','#00FF00','#0000FF']
gw = GWindow(max,max)
x = 0
for color in colors:
r = GRect(x, 0, max // 4, max) # stripe
r.set_filled(True)
r.set_color(color) # color it in
gw.add(r)
x += max // 4 # move over
|
# take some colors, in red/green/blue, like:
# [255, 0, 0]
# and make a hexcode, like
# '#FF0000'
def to_color(rgbs):
# take some colors, in red/green/blue, like:
# [255, 0, 0]
# and make a hexcode, like
# '#FF0000'
def to_color(rgbs:list) -> str:
# e.g. [255, 0, 0] -> '#FF0000'
def to_color(rgbs:list[int]) -> str:
# e.g. [255, 0, 0] -> '#FF0000'
def to_color(rgbs:list[int]) -> str:
hexes = [hex(brightness) for brightness in rgbs]
>>> rgbs = [255,100,0]
>>> hexes = [hex(brightness) for brightness in rgbs]
>>> hexes
['0xff', '0x64', '0x0']
def to_color(rgbs:list[int]) -> str:
hexes = [hex(brightness) for brightness in rgbs]
hexes = [color.replace('x','0') for color in hexes]
>>> hexes = [color.replace('x','0') for color in hexes]
>>> hexes
['00ff', '0064', '000']
def to_color(rgbs:list[int]) -> str:
hexes = [hex(brightness) for brightness in rgbs]
hexes = [color.replace('x','0') for color in hexes]
hexes = [color[-2:] for color in hexes] # last two
>>> hexes = [color[-2:] for color in hexes]
>>> hexes
['ff', '64', '00']
def to_color(rgbs:list[int]) -> str:
hexes = [hex(brightness) for brightness in rgbs]
hexes = [color.replace('x','0') for color in hexes]
hexes = [color[-2:] for color in hexes] # last two
return '#' + "".join(hexes)
>>> to_color([100,0xff,255])
'#64ffff'
def to_color(rgbs:list[int]) -> str:
return '#' + "".join([hex(c).replace('x','0')[-2:] for c in rgbs])
>>> to_color([0,16,0x20])
'#001020'
from pgl import *
max = 255 # better than 400 - 255 is max brightness
dbg = False # I had to use this a lot
gw = GWindow(max,max)
def my_rect(x,y,w,h,color):
rect = GRect(x,y,w,h)
rect.set_filled(True)
rect.set_color(color)
gw.add(rect)
def to_color(rgbs:list[int]) -> str:
return '#' + "".join([hex(c).replace('x','0')[-2:] for c in rgbs])
def pixel(x,y,color):
my_rect(x,y,1,1,color)
for i in range(max):
for j in range(max):
pixel(i,j,to_color([i,0,0]))
[[0, 1, 2, ... , 254, 255],
[0, 1, 2, ... , 254, 255],
< 252 more such lists >
[0, 1, 2, ... , 254, 255]] |
|
def pixel(x,y,color):
my_rect(x,y,1,1,color)
[[pixel(i,j,to_color([i,j,0])) for j in range(max)] for i in range(max)]
|
|
GImage
GImage(filename)
fish.jpg
fish.png
GImage
gw = GWindow(800, 550)
image = GImage("Pixel-example.png") # GNU Free
image.scale(gw.get_width() / image.get_width())
gw.add(image)
# can look at pixels too if you want - this will print A LOT
print(image.get_pixel_array()) # this isn't neat rbg lists
# more on pixel arrays next time