Colors V

‘Colors V’ is the fifth version of my ‘Colors’ program. The four prior versions I just did not publish. This fifth version is nice and cleaned up, using easy to manipulate code and user input.
It is designed to create gradients. Going through the screen, column by column, it calculates for every pixel a color (see first function in the code). By this method it sometimes creates beautiful, sometimes more – I would say ‘interesting’ – images. The image I show here is a very basic gradient (there are way more interesting results).

Usage

  • ‘s’ to take a screenshot
  • Space to recreate (or start) the image creation

The First Example


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 23rd of March, 2015
#         edited  2nd of April, 2015

This part of the code defines the image (if I post other results, I will just update this part of the code and referr to the rest).

# creates pixel (used in array manipulation)
def getPixel(x, y, X, Y):
	return (
		colorValid( x ),
		colorValid( y ),
		colorValid( 0 )
	)

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
	newColor = math.fabs(_color)
	n = _max - _min
	
	if newColor > n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

# tick function
def tick():
	
	# handle events
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
				sys.exit()
		
		if event.type == pygame.KEYDOWN:
			
			# save current display
			if event.key == keys.screenshot:
				if not os.path.exists(os.getcwd() + "/screenshots/"):
					os.mkdir(os.getcwd() + "/screenshots")
				n = 1
				while os.path.exists(os.getcwd() + "/screenshots/" + str(n) + ".png"):
					n += 1
				pygame.image.save(main.SCREEN, os.getcwd() + "/screenshots/" + str(n) + ".png")
				
				print "SAVED"
			
			# reload image
			if event.key == keys.reload:
				main.SURF.fill([0, 0, 0])
				main.COLORTICKS = 0
				main.COLOR = True
	
	if main.COLOR:
		# 'main.COLORTICKS' defines the pixel column
		main.COLORTICKS += 1
		if main.COLORTICKS > main.WIDTH:
			main.COLOR = False
		
		# make the array
		array = pygame.PixelArray(main.SURF)
		
		# change the array (line by line for performance)
		for Y in range(0, main.HEIGHT):
			X = (main.COLORTICKS - 1) % main.WIDTH
		
			# scaling
			x = 255. / main.WIDTH  * X
			y = 255. / main.HEIGHT * Y
			
			# fill array
			array[X][Y] = getPixel(x, y, X, Y)
		
		# delete the array
		del array

# render function
def render():
	main.SCREEN.fill([0, 0, 0])
	
	main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
	pygame.display.flip()

# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			#print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# dummy class for global variables
class dummy():
	pass

# importing needed modules
import pygame, sys, time, math, os, random

# main dummy
main = dummy()
main.TICKS = 0

main.RESOLUTION = 1
main.WIDTH = 1080 / main.RESOLUTION
main.HEIGHT = 720 / main.RESOLUTION
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SURF = pygame.Surface(main.SIZE)

main.SCALE = 1 * main.RESOLUTION
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]

main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Colors V")

main.COLORTICKS = 0
main.COLOR = False

# key dummy
keys = dummy()
keys.screenshot = pygame.K_s
keys.reload = pygame.K_SPACE

# start program
run()
Advertisements

5 thoughts on “Colors V

  1. Pingback: Colors V – other results | J-Blog

  2. Pingback: Colors V – other results 2 | J-Blog

  3. Pingback: Colors V – reupload | J-Blog

  4. Pingback: Rand Pix | J-Blog

  5. Pingback: Colors VI | J-Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s