-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypnic1.py
1623 lines (1381 loc) · 71.8 KB
/
hypnic1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from pathlib import Path
import math
import random
from tkinter import *
from PIL import Image, ImageTk
import PIL
import imageio
# VIDEO FUNCTIONALITY PLANNED FOR FUTURE
#import cv2
# THREADING FUNCTIONALITY PLANNED FOR FUTURE (https://pythonprogramming.net/threading-tutorial-python/)
#import threading
#from queue import Queue
#import time
"""
_________________________________________________
#TODO
_________________________________________________
- ABSTRACTION
- Build a system to allow random image manipulation functions to be selected and assigned random inputs
- Abstract this to its own file
- POSSIBLY abstract GIF creation into its own file
- POSSIBLY abstract image manipulation into its own file, and just have hypnic1.py be GUI and main controls?
- For functions like ImageManipulataor.{hueShitf()/saturationShift()valueShift()}, add an optional boolean to the input
- so that the user can specify whether or not they want values to min/max out at the bounds of that variable instead
- of undergoing the current process with the modulus operator
- Implement an rgbShift() or {r/g/b}Shift() function and give it that same optional boolean functionality
- Implement GIF_MODE variable as-described in the comments (DONE, but children listed below are incomplete)
- Add control for whether or not the output of each manipulation is saved,
as it is wasteful/unnecessary depending on GIF_MODE
- Implement ability to perform operations based on neighboring pixels
- Will require changing how rgbResult is initialized in ImageManipulator.rgbFunc()
= possibly need to implement a new ImageManipulator member variable called imageCurrent, analogous to
imageIn and imageOut
- Add functionality for video output instead of just animated GIFs
- Update GUI to allow users to interactively apply filters and functions, with deep control over the underlying math
as well as the ability to render in various formats
- Condense Window class' createOutputImage(), createGif() and createVideo() code to avoid redundancy
"""
# GLOBAL CONSTANTS
# ________________________________
# GUI-RELATED VARIABLES
# Whether or not to use the GUI at all
# If disabled, then the program will simply create an image/gif/video based on other global variable values without
# allowing specific control over parameters
ENABLE_GUI = False
# GENERAL VARIABLES
# Whether or not the image should be manipulated at all
# Can be disabled, for example, in situations when non-manipulation functionality is being tested
MANIPULATE_IMAGE = True
# If STOP_MANIPULATING_AFTER is set to 0 or below then all manipulations define in rgbFunc() will be performed
# If it is set to a positive integer then no manipulation with a higher index than STOP_MANIPULATING_AFTER will be done
STOP_MANIPULATING_AFTER = 2
# Path to the image used as program input
INPUT_IMG = "github-profile-image.png"
# Path at which the resulting image will be saved
OUTPUT_IMG = "output\\outputLEAN1"
OUTPUT_IMG_EXTENSION = ".jpg"
# Whether every manipulation pass should cover a random range of the image (as opposed to the entire frame)
RANDOMIZE_MANIPULATION_POSITIONS = False
# If randomizing manipulation positions, defines the minimum and maximum boundary positions for a manipulation area
# Defined as a fraction of the entire image dimension along the respective axis
RANDOM_MIN_X_EDGE = 0.0
RANDOM_MAX_X_EDGE = 1.0
RANDOM_MIN_Y_EDGE = 0.0
RANDOM_MAX_Y_EDGE = 1.0
# If randomizing manipulation positions, defines the minimum and maximum dimensions for a manipulation area
# Defined as a fraction of the entire image dimension along the respective axis
RANDOM_MIN_X_DIM = 0.5
RANDOM_MAX_X_DIM = 0.9
RANDOM_MIN_Y_DIM = 0.5
RANDOM_MAX_Y_DIM = 0.9
# If True, then each generated image from sequential values of manip_index in ImageManipulator.rgbFunc() will be
# applied to the output of the previous call of ImageManipulator.rgbFunc()
# If False, then each generated image from sequential values of manip_index in ImageManipulator.rgbFunc() will be
# applied to the input image, effectively causing zero interaction between different manip_index values
MANIPULATE_PREVIOUS_OUTPUT = True
# How many times to repeat the entire image manipulation process
# In the case where MANIPULATE_PREVIOUS_OUTPUT == True, then when a new round of manipulation begins, the final output
# image of the last round of manipulation is used as the base image in the new round of manipulation
# In the case where MANIPULATE_PREVIOUS_OUTPUT == False, then this isn't a useful variable as it just creates copies
# of images that have already been created
NUM_ROUNDS_OF_MANIPULATION = 1
# Determines whether to use the manipulation order as listed in rgb(func) or to randomize the order
RANDOM_MANIPULATION_ORDER = False
# GIF/VIDEO-RELATED VARIABLES
# VIDEO RENDERING FUNCTIONALITY HAS NOT YET BEEN COMPLETED
# Whether or not to generate an animated GIF from all rendered images
CREATE_GIF = False
# Whether or not to generate a video from all rendered images
CREATE_VIDEO = False
# Path at which the resulting animated GIF will be saved, if CREATE_GIF = True
GIF_PATH = "output\\outputLEAN1.gif"
# Path at which the resulting video will be saved, if CREATE_VIDEO = True
VIDEO_PATH = "output\\video2.avi"
# The number of seconds for which each frame of the GIF will be displayed
GIF_SECONDS_PER_FRAME = 0.2
# Whether or not to play the animation (GIF/video) frames in reverse when the end is reached, transitioning back to the
# original source image instead of abruptly jumping right back to the start
REVERSE_ANIMATION_AT_END = True
# Number of times to repeat each rendered image during forward animated GIF progression
# Effectively lengthens the time for which each frame is visible in the animated GIF
GIF_FRAMES_PER_IMAGE_FORWARD = 1
# Number of times to repeat each rendered image during reverse animated GIF progression
GIF_FRAMES_PER_IMAGE_REVERSE = 1
# Number of times to repeat each rendered image during forward video progression
# Effectively lengthens the time for which each frame is visible in the forward progression of the animated GIF
VIDEO_FRAMES_PER_IMAGE_FORWARD = 1
# Number of times to repeat each rendered image during reverse video progression
# Effectively lengthens the time for which each frame is visible in the reverse progression of the animated GIF
VIDEO_FRAMES_PER_IMAGE_REVERSE = 1
# The total number of frames to use in a input-to-final-output transition GIF (for example, GIF_MODE values 1/2/3)
ANIMATION_NUM_TRANSITION_FRAMES = 20
# TODO: This Window class and the entire GUI within hypnic1.py are completely deprecated. I need to eventually
# switch to using the GUI that I've been building within hypnic_gui.py and all of its associated files.
# However, this is such a large change that I may end up essentially pulling hypnic1.py's manipulation content
# into the newly built environment instead. This is probably the best practice for long-term readability as well,
# given how much better I've been at keeping things organized and abstracted in this new attempt towards building a GUI
# tkinter instance for GUI display and user interaction related to manipulation of images
# based heavily on code from http://pythonprogramming.net by Sentdex
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
self.manipulator = ImageManipulator()
# Creation of window
def init_window(self):
# Change the title of the master widget
self.master.title("hypnic-functions")
# Allow the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# Create a button instance
quitButton = Button(self, text="Quit", command=self.client_exit)
# Place the button on the window
quitButton.place(x=0, y=0)
# Create a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# Create the file object
file = Menu(menu)
# Add a command called "Exit" to the menu option, which runs the function self.client_exit()
file.add_command(label="Exit", command=self.client_exit)
# Add "File" to the menu
menu.add_cascade(label="File", menu=file)
# Create the file object
edit = Menu(menu)
# Add a command called "Undo" to the menu option
edit.add_command(label="Undo")
# Add "Edit" to the menu
menu.add_cascade(label="Edit", menu=edit)
# Add commands to the menu for doing the following:
# Displaying the input image, generating/displaying the output image, showing text, creating an animated GIF,
# and creating a video
edit.add_command(label="Show Input Image", command=self.showInputImage)
edit.add_command(label="Show Output Image", command=self.showOutputImage)
edit.add_command(label="Create GIF", command=self.createGIF)
edit.add_command(label="Create Video", command=self.createVideo)
# Displays the input image
def showInputImage(self):
# Load the input image
load = Image.open(INPUT_IMG)
render = PIL.ImageTk.PhotoImage(load)
# Display the input image
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
# Display some text related to displaying the input image
text = Label(self, text="Input Image:")
text.pack()
# Shows the output image
# If the output image hasn't already been created, creates it first
def showOutputImage(self):
if not self.manipulator.outputImageReady:
# Display some text related to generating the output image
# NOTE: This text display functionality is currently broken.
# Need to research tkinter text display more in order to determine why
text = Label(self, text="Generating the output image... Please wait!")
text.pack()
self.createOutputImage()
# Load the output image
load = Image.open(self.manipulator.outputImagePath)
render = PIL.ImageTk.PhotoImage(load)
# Display the output image
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
# Display some text related to displaying the output image
text = Label(self, text="Output Image:")
text.pack()
# Generates the output image if it hasn't already been created
def createOutputImage(self):
# Checks whether image manipulation has been enabled and responds accordingly
if not MANIPULATE_IMAGE:
# Display text notifying the user that image manipulation is disabled
text = Label(self, text="ERROR: Image manipulation is disabled!")
text.pack()
else:
# Checks if the output image has already been created
if not self.manipulator.outputImageReady:
# Tell the ImageManipulator object to perform the manipulation routine
self.manipulator.manipulate()
text = Label(self, text="Generating the output image... Please wait!")
text.pack()
# Displays the output image
self.showOutputImage()
# Generates the output image if it hasn't already been created
def createGIF(self):
# Checks whether image manipulation has been enabled and responds accordingly
if not MANIPULATE_IMAGE:
# Display text notifying the user that image manipulation is disabled
text = Label(self, text="ERROR: Can't create GIF because image manipulation is disabled!")
text.pack()
elif not CREATE_GIF:
# Display text notifying the user that animated GIF creation is disabled
text = Label(self, text="ERROR: Animated GIF creation is disabled!")
text.pack()
else:
# Checks if the output image has already been created
if not self.manipulator.outputImageReady:
# Tell the ImageManipulator object to perform the manipulation routine
self.manipulator.manipulate()
text = Label(self, text="Generating the output image... Please wait!")
text.pack()
# Checks if the animated GIF has already been created
if not self.manipulator.gifReady:
# Tell the ImageManipulator object to perform the animated GIF creation routine
text = Label(self, text="Generating the animated GIF... Please wait!")
text.pack()
self.manipulator.generateGIF()
else:
text = Label(self, text="Animated GIF has already been created!")
text.pack()
self.showOutputImage()
def createVideo(self):
# Checks whether image manipulation has been enabled and responds accordingly
if not MANIPULATE_IMAGE:
# Display text notifying the user that image manipulation is disabled
text = Label(self, text="ERROR: Can't create GIF because image manipulation is disabled!")
text.pack()
elif not CREATE_VIDEO:
# Display text notifying the user that video creation is disabled
text = Label(self, text="ERROR: Video creation is disabled!")
text.pack()
else:
# Checks if the output image has already been created and if not, creates it
if not self.manipulator.outputImageReady:
# Tell the ImageManipulator object to perform the manipulation routine
self.manipulator.manipulate()
text = Label(self, text="Generating the output image... Please wait!")
text.pack()
# Checks if the video has already been created
if not self.manipulator.videoReady:
# Tell the ImageManipulator object to perform the video creation routine
text = Label(self, text="Video generation has not yet been implemented!")
text.pack()
self.manipulator.generateGIF()
else:
text = Label(self, text="Video has already been created!")
text.pack()
# Exit of window
def client_exit(self):
exit()
# Container class for holding all variables and functions related to manipulation of images
class ImageManipulator:
def __init__(self):
# MEMBER VARIABLES UPON INITIALIZATION
# The image to modify
self.imageIn = Image.open(INPUT_IMG)
# A copy of the input image to which the functions are applied
self.imageOut = Image.open(INPUT_IMG)
# A copy of the most recently saved output image, used for reference when using information
# from multiple pixels at once
self.imageReference = Image.open(INPUT_IMG)
# The X resolution of self.imageIn
self.xRes = self.imageIn.size[0]
# The Y resolution of self.imageIn
self.yRes = self.imageIn.size[1]
# The X value of the current pixel being edited
self.currentX = 0
# The Y value of the current pixel being edited
self.currentY = 0
# Suffix to apply to the filename of the current version of self.imageOut upon running self.renderOutputImage()
self.currentImageIndex = 1
# List of file paths of all rendered images
self.outputFileList = []
# An array of pixels representing the input image. Used for reference but never modified.
self.pixelsIn = self.imageIn.load()
# An array of pixels representing the output image. Initialized identical to self.pixelsIn
# Modified over time while iterating through rows/columns. Should not be used for reference.
self.pixelsOut = self.imageOut.load()
# An array of pixels representing the reference image
self.pixelsReference = self.imageReference.load()
# Tracks when the first pixel of a reference image is going to be used in a new function application
self.referenceStarting = True
# Tracks when all image manipulation routines are complete
self.manipulationComplete = False
# Used to determine the total number of manipulations that are configured in the current code
# Initialized as negative to make it an invalid input
self.numTotalManipulations = -1
# Holds a list of all valid values of manip_index
self.manipulationsList = []
# Holds a list of colors which can be used as input for various functions, as RGB values
self.colorList = []
# Holds the HSV value of each RGB color in colorList
self.colorListHSV = []
# Path to which the output image is saved
self.outputImagePath = Path("")
# Tracks whether or not the output image is ready to be displayed
self.outputImageReady = False
# Holds paths of all images to be used in GIF and/or video creation
self.frames = []
# Tracks the output file number at which a transition-type GIF/video should begin
self.transitionStartIndex = 0
# Sets the animation mode for GIF/video creation
self.animationMode = 3
# Tracks whether or not the output animated GIF has been created
self.gifReady = False
# Holds imageio variables which reference the contents of self.frames for animated GIF creation
self.imageioFrames = []
# Tracks whether or not the output video has been created
self.videoReady = False
# Internal variable used to track error incidences during debugging
self.errorCount = 0
self.prepareDirectories()
# Converts an RGB color value to an HSV color value
# Based on algorithm (with modified domain) from:
# http://coecsl.ece.illinois.edu/ge423/spring05/group8/finalproject/hsv_writeup.pdf
# R, G, and B are integers from 0 to 255 inclusive
# H, S, and V are each measured on a continuous scale
# H, conceptually, is measured in degrees and ranges from 0 <= H < 360
# S is measured from 0 to 1 inclusive
# The lower S is, the more gray is present, causing it to appear faded
# V is measured from 0 to 1 inclusive
# V represents brightness, where 0 is fully dark and 1 is fully bright
# If V is 0, then the color is always black, regardless of H or S
@staticmethod
def fromRGBtoHSV(rgb):
minRGB = float(min(rgb))
maxRGB = float(max(rgb))
deltaRGB = maxRGB - minRGB
h = 0
s = 0
v = maxRGB / 255
# r == g == b == 0
if maxRGB == 0:
return (h, s, v)
else:
s = deltaRGB / maxRGB
# Hue is null
if deltaRGB == 0:
return (h, s, v)
# Hue is non-null
else:
# Hue is between yellow and magenta
if rgb[0] == maxRGB:
h = round(60 * ((rgb[1] - rgb[2]) / (deltaRGB)))
# Hue is between cyan and yellow
elif rgb[1] == maxRGB:
h = round(60 * (2 + ((rgb[2] - rgb[0]) / (deltaRGB))))
# Hue is between magenta and cyan
else:
h = round(60 * (4 + ((rgb[0] - rgb[1]) / (deltaRGB))))
# Ensure that Hue is in the 0 <= H < 360 range
h %= 360
return (h, s, v)
# Converts an HSV color value to an RGB color value
# Based on algorithm (with modified domain) from:
# https://www.rapidtables.com/convert/color/hsv-to-rgb.html
# R, G, and B are integers from 0 to 255 inclusive
# H, S, and V are each measured on a continuous scale
# H is measured in degrees on the domain of 0 <= H < 360
# S and V range from 0 to 1 inclusive
@staticmethod
def fromHSVtoRGB(hsv):
c = hsv[1] * hsv[2]
x = c * (1 - abs((hsv[0] / 60.0) % 2 - 1))
m = hsv[2] - c
if hsv[0] < 180:
if hsv[0] < 120:
# 0 <= H < 60
if hsv[0] < 60:
rgb = [c, x, 0]
# 60 <= H < 120
else:
rgb = [x, c, 0]
# 120 <= H < 180
else:
rgb = [0, c, x]
else:
# 180 <= H < 240
if hsv[0] < 240:
rgb = [0, x, c]
else:
# 240 <= H < 300
if hsv[0] < 300:
rgb = [x, 0, c]
# 300 <= H < 360
else:
rgb = [c, 0, x]
rgb[0] = int(round(255 * (rgb[0] + m)))
rgb[1] = int(round(255 * (rgb[1] + m)))
rgb[2] = int(round(255 * (rgb[2] + m)))
return tuple(rgb)
# Returns the nearest integer to the distance between two X/Y coordinate pairs
@staticmethod
def calcDist(x1, y1, x2, y2):
return round(math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2))
# Defines an algebraic function on the cartesian plane
# Takes an X value as input and returns the Y value at X on that algebraic function
# NOTE: Currently returns an error if slope is too negative or y intercept is too low
# NEED TO INVESTIGATE WHY THIS HAPPENS
@staticmethod
def calcCartesianFunc(xIn, slope, yIntercept):
yOut = round(xIn * slope + yIntercept)
return yOut
# Swaps the Saturation and Value values for a pixel
def modFlipSV(self, rgbIn):
hsvIn = self.fromRGBtoHSV(rgbIn)
hsvOut = (hsvIn[0], hsvIn[2], hsvIn[1])
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Moves Saturation and Value values closer together by a given percentage factor of their difference
# A factor of 100 (100%) leads to Saturation being equal to Value, specifically the two being equal to the average
# of their original values
# If a negative factor is provided, the two are moved away from each other by the same magnitude which would be
# present if they were moving closer together
# To prevent results which lead to invalid output ranges, the final values have a modulus of 1 applied at the end
def modSlideSV(self, rgbIn, factor):
rgbOut = rgbIn
hsvIn = self.fromRGBtoHSV(rgbIn)
if hsvIn[1] < hsvIn[2]:
hsvOut = (hsvIn[0], hsvIn[1] + math.fabs(hsvIn[2] - hsvIn[1]) * (float(factor) / 2) % 1,
hsvIn[2] - math.fabs(hsvIn[2] - hsvIn[1]) * (float(factor) / 2) % 1)
elif hsvIn[2] < hsvIn[1]:
hsvOut = (hsvIn[0], hsvIn[1] - math.fabs(hsvIn[1] - hsvIn[2]) * (float(factor) / 2) % 1,
hsvIn[2] + math.fabs(hsvIn[2] - hsvIn[1]) * (float(factor) / 2) % 1)
else:
return rgbIn
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Shifts the Hue value by a given number of degrees
def modHueShift(self, rgbIn, shift):
hsvIn = self.fromRGBtoHSV(rgbIn)
hsvOut = ((hsvIn[0] + shift) % 360, hsvIn[1], hsvIn[2])
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Shifts the Saturation value by a given number of degrees
def modSaturationShift(self, rgbIn, shift):
hsvIn = self.fromRGBtoHSV(rgbIn)
hsvOut = (hsvIn[0], (hsvIn[1] + shift) % 1, hsvIn[2])
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Shifts the Value value by a given number of degrees
def modValueShift(self, rgbIn, shift):
hsvIn = self.fromRGBtoHSV(rgbIn)
hsvOut = (hsvIn[0], hsvIn[1], (hsvIn[2] + shift) % 1)
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Sets the Saturation value to a multiple of its previous value
def modSaturationMultiple(self, rgbIn, multiple):
hsvIn = self.fromRGBtoHSV(rgbIn)
hsvOut = (hsvIn[0], (hsvIn[1] * multiple) % 1, hsvIn[2])
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Sets the Value value to a multiple of its previous value
def modValueMultiple(self, rgbIn, multiple):
hsvIn = self.fromRGBtoHSV(rgbIn)
hsvOut = (hsvIn[0], hsvIn[1], (hsvIn[2] * multiple) % 1)
rgbOut = self.fromHSVtoRGB(hsvOut)
return rgbOut
# Returns a color offset from rgbIn towards rgbGoal
# Red, Green, and Blue values are separately modified by a number such that if it were repeated stepsRemaining
# times, then the rgbGoal color values would be reached
# Designed for use with Animation Mode 3, but has other potential applications as well
@staticmethod
def transitionRGB(rgbIn, rgbGoal, stepsRemaining):
rgbOut = list(rgbIn)
rgbOut[0] = rgbIn[0] + round(float(rgbGoal[0] - rgbIn[0]) / float(stepsRemaining))
rgbOut[1] = rgbIn[1] + round(float(rgbGoal[1] - rgbIn[1]) / float(stepsRemaining))
rgbOut[2] = rgbIn[2] + round(float(rgbGoal[2] - rgbIn[2]) / float(stepsRemaining))
return tuple(rgbOut)
# Rotates the R/G/B values of a pixel by 1
@staticmethod
def modRotate1RGB(rgbIn):
rgbOut = (rgbIn[1], rgbIn[2], rgbIn[0])
return rgbOut
# Rotates the R/G/B values of a pixel by 2
@staticmethod
def modRotate2RGB(rgbIn):
rgbOut = (rgbIn[2], rgbIn[0], rgbIn[1])
return rgbOut
# Swaps the R and B values of a pixel
@staticmethod
def modFlipRGB(rgbIn):
rgbOut = (rgbIn[2], rgbIn[1], rgbIn[0])
return rgbOut
# Swaps the G and B values of a pixel
@staticmethod
def modFlipRotate1RGB(rgbIn):
rgbOut = (rgbIn[0], rgbIn[2], rgbIn[1])
return rgbOut
# Swaps the R and G values of a pixel
@staticmethod
def modFlipRotate2RGB(rgbIn):
rgbOut = (rgbIn[1], rgbIn[0], rgbIn[2])
return rgbOut
# Test function to edit the RGB values of a pixel based on a defined algebraic function
def modDistFromCartesianFunc(self, rgbIn):
maxDist = self.calcDist(0, 0, self.xRes, self.yRes)
cartesianDist = self.calcDist(self.currentX,
self.currentY,
self.currentX,
self.calcCartesianFunc(self.currentX))
distRatio = (cartesianDist / maxDist)
rgbOut = ((round(rgbIn[0] * distRatio)) % 255,
(round(rgbIn[1] * distRatio)) % 255,
(round(rgbIn[2] * distRatio)) % 255)
return rgbOut
# Another est function to edit the RGB values of a pixel based on a defined algebraic function
def modDistFromCartesianFunc2(self, rgbIn):
maxDist = self.calcDist(0, 0, self.xRes, self.yRes)
cartesianDist = self.calcDist(self.currentX,
self.currentY,
self.currentX,
self.calcCartesianFunc(self.currentX))
distRatio = (cartesianDist / maxDist)
rgbOut = (((((round(rgbIn[0] * distRatio)) * 0.1) % 255) + rgbIn[0]) % 255,
((((round(rgbIn[1] * distRatio)) * 0.1) % 255) + rgbIn[1]) % 255,
((((round(rgbIn[2] * distRatio)) * 0.1) % 255) + rgbIn[2]) % 255)
return rgbOut
# Allows a R, G, or B value to be shifted using an algebraic function, if it falls below a lower bound or above an
# upper bound. Magnitude of shift is calculated based on a linear equation. Separate slope and Y-intercept
# values are used depending on whether the input R/G/B value falls below or above a specified bound
@staticmethod
def calcFromCustomDomainRGB(valIn, lowerBound, upperBound, yIntBelow, slopeBelow, yIntAbove, slopeAbove):
valOut = valIn
if valIn <= lowerBound:
dist = valOut - lowerBound
valOut += slopeBelow * dist + yIntBelow
elif valIn >= upperBound:
dist = upperBound - valOut
valOut += slopeAbove * dist + yIntAbove
valOut %= 255
valOut = int(round(valOut))
return valOut
# Allows a H value to be shifted using an algebraic function, if it falls below a lower bound or above an
# upper bound. Magnitude of shift is calculated based on a linear equation. Separate slope and Y-intercept
# values are used depending on whether the input H value falls below or above a specified bound
@staticmethod
def calcFromCustomDomainH(valIn, lowerBound, upperBound, yIntBelow, slopeBelow, yIntAbove, slopeAbove):
valOut = valIn
if valIn <= lowerBound:
dist = valOut - lowerBound
valOut += slopeBelow * dist + yIntBelow
elif valIn >= upperBound:
dist = upperBound - valOut
valOut += slopeAbove * dist + yIntAbove
valOut %= 360
valOut = int(round(valOut))
return valOut
# Allows a S or V value to be shifted using an algebraic function, if it falls below a lower bound or above an
# upper bound. Magnitude of shift is calculated based on a linear equation. Separate slope and Y-intercept
# values are used depending on whether the input S/V value falls below or above a specified bound
@staticmethod
def calcFromCustomDomainSV(valIn, lowerBound, upperBound, yIntBelow, slopeBelow, yIntAbove, slopeAbove):
valOut = valIn
if valIn <= lowerBound:
dist = valOut - lowerBound
valOut += slopeBelow * dist + yIntBelow
elif valIn >= upperBound:
dist = upperBound - valOut
valOut += slopeAbove * dist + yIntAbove
valOut %= 1
valOut = int(round(valOut))
return valOut
# Sets the value of self.colorListHSV based on the current value of self.colorList
# Returns self.colorListHSV as well, but the function will set the self.colorList variable regardless
def generateColorListHSV(self):
newList = []
for color in self.colorList:
newList.append(self.fromRGBtoHSV(color))
self.colorListHSV = newList
return self.colorListHSV
# Sets self.colorList to a list of RGB color tuples of length numColors,
# transitioning from color1 to color2 via regular steps in R/G/B values
# Returns self.colorList as well, but the function will set the self.colorList variable regardless
def colorListTransitionRGB(self, numColors, color1, color2):
newList = []
for i in range(numColors):
newList.append((round(color1[0] + (i * (color2[0] - color1[0])) / (numColors - 1)),
round(color1[1] + (i * (color2[1] - color1[1])) / (numColors - 1)),
round(color1[2] + (i * (color2[2] - color1[2])) / (numColors - 1))))
self.colorList = newList
self.generateColorListHSV()
return self.colorList
# Sets self.colorList to a list of RGB color tuples of length numColors,
# transitioning from color1 to color2 via regular steps in H/S/V values
# Returns self.colorList as well, but the function will set the self.colorList variable regardless
def colorListTransitionHSV(self, numColors, color1, color2):
hsvStart = self.fromRGBtoHSV(color1)
hsvEnd = self.fromRGBtoHSV(color2)
hsvColors = []
for i in range(numColors):
hsvColors.append((round(hsvStart[0] + (i * (hsvEnd[0] - hsvStart[0])) / (numColors - 1)),
round(hsvStart[1] + (i * (hsvEnd[1] - hsvStart[1])) / (numColors - 1)),
round(hsvStart[2] + (i * (hsvEnd[2] - hsvStart[2])) / (numColors - 1))))
newList = []
for hsvColor in hsvColors:
newList.append(self.fromHSVtoRGB(hsvColor))
self.colorList = newList
self.generateColorListHSV()
return self.colorList
# Sets self.colorList to list of size^3 colors, spanning a cubic grid from R/G/B == rgbMin to R/G/B == rgbMax
# Returns self.colorList as well, but the function will set the self.colorList variable regardless
def colorListCubeRGB(self, size, rgbMin = 0, rgbMax = 255):
# Takes the modulus of rgbMin and rgbMax to ensure that they fall within acceptable values
# The algorithm still functions even if rgbMin is greater or equal to rgbMax
rgbMin %= 255
rgbMax %= 255
# A list of the possible values that each of R, G, and B value on the grid
rgbVals = []
for i in range(size):
if i == 0:
rgbVals.append(round(rgbMin))
else:
rgbVals.append(round(rgbMin + (rgbMax - rgbMin) / i))
newList = []
for r in range(size):
for g in range(size):
for b in range(size):
newList.append((rgbVals[r], rgbVals[g], rgbVals[b]))
self.colorList = newList
self.generateColorListHSV()
return self.colorList
# Sets self.colorList to a list, of length numColors, of random RGB color tuples
# color1 and color2 optionally act as boundaries for maximum R/G/B values
# Returns self.colorList as well, but the function will set the self.colorList variable regardless
def colorListRandom(self, numColors, color1 = (0, 0, 0), color2 = (255, 255, 255)):
newList = []
minR = min(color1[0], color2[0])
maxR = max(color1[0], color2[0])
minG = min(color1[1], color2[1])
maxG = max(color1[1], color2[1])
minB = min(color1[2], color2[2])
maxB = max(color1[2], color2[2])
for i in range(numColors):
newList.append((random.randint(minR, maxR), random.randint(minG, maxG), random.randint(minB, maxB)))
self.colorList = newList
self.generateColorListHSV()
return self.colorList
# Modifies the redness/greenness/blueness of colors in self.colorList
# The modifier value evenly transitions from redStart to redEnd, etc.
# if "bound" is set to True, then values will be bounded from 0 to 255. Otherwise, the modulus is used.
def rgbShiftColorList(self, redStart, redEnd, greenStart, greenEnd, blueStart, blueEnd, bound = False):
newList = []
numColors = len(self.colorList)
for i in range(numColors):
newRed = round(self.colorList[i][0] + ((i * (redEnd - redStart)) / (numColors - 1)))
newGreen = round(self.colorList[i][1] + ((i * (greenEnd - greenStart)) / (numColors - 1)))
newBlue = round(self.colorList[i][2] + ((i * (blueEnd - blueStart)) / (numColors - 1)))
rgbTupleList = [newRed, newGreen, newBlue]
for i in range(len(rgbTupleList)):
if bound:
if rgbTupleList[i] > 255:
rgbTupleList[i] = 255
elif rgbTupleList[i] < 0:
rgbTupleList[i] = 0
else:
rgbTupleList[i] = rgbTupleList[i] % 255
newList.append(tuple(rgbTupleList))
self.colorList = newList
return 0
# Modifies the H/S/V of colors in self.colorList
# The modifier value evenly transitions from hStart to hEnd, etc.
# if "bound" is set to True, then values will be bounded as follows. Otherwise, the modulus is used.
# 0 <= H <= 360
# 0 <= S <= 1
# 0 <= V <= 1
def hsvShiftColorList(self, hStart, hEnd, sStart, sEnd, vStart, vEnd, bound=False):
newList = []
numColors = len(self.colorList)
for i in range(numColors):
hsv = self.fromRGBtoHSV(self.colorList[i])
newH = round(hsv[0] + ((i * (hEnd - hStart)) / (numColors - 1)))
newS = hsv[1] + ((i * (sEnd - sStart)) / (numColors - 1))
newV = hsv[2] + ((i * (vEnd - vStart)) / (numColors - 1))
hsvTupleList = [newH, newS, newV]
upperBoundList = [360, 1.0, 1.0]
for i in range(len(hsvTupleList)):
if bound:
if hsvTupleList[i] > upperBoundList[i]:
hsvTupleList[i] = upperBoundList[i]
elif hsvTupleList[i] < 0:
hsvTupleList[i] = 0
else:
hsvTupleList[i] = hsvTupleList[i] % upperBoundList[i]
newColor = self.fromHSVtoRGB(tuple(hsvTupleList))
newList.append(newColor)
self.colorList = newList
return 0
# Sets a pixel's color to a member of self.colorList, sequentially based on average value of R, G, and B
# TODO: Write a new version of that function which uses closest total RGB distance from input list
# TODO: Write a new version of this function which uses closest value distance from input list
def limitColorsByAverageRGB(self, rgbIn):
if not self.colorList:
exit(421)
average = (rgbIn[0] + rgbIn[1] + rgbIn[2]) / 3
numColors = len(self.colorList)
colorOutIndex = -1
for n in range(numColors):
if (average >= 255 * (n / numColors)) and (average < 255 * ((n + 1) / numColors)):
colorOutIndex = n
if colorOutIndex == -1:
exit(42)
else:
return self.colorList[colorOutIndex]
# Treats colors as points on a 3D coordinate grid (r/g/b ~ x/y/z) and outputs the color
# which is least distant from the input color
# TODO: Write a similar function but for distance within HSV space (polar coordinates with Z axis, H is theta)
# TODO: Improve efficiency by treating color list sometimes as a cube and narrowing the search space to at most
# somewhere between 8 and 27 values
def limitColorsByMatchRGB(self, rgbIn):
if not self.colorList:
exit(491)
# Stores the R value of the input color
rVal = rgbIn[0]
gVal = rgbIn[1]
bVal = rgbIn[2]
# Stores the distance of the color (within self.colors) with the closest R value to the input color
# Initialized as the difference between the maximum and minimum possible values of R
# The distance between (0, 0, 0) and (255, 255, 255) is approximately 441.673
minDist = 442.0
# Stores the index (within self.colors) of the color which most closely matches the input color
colorOutIndex = -1
numColors = len(self.colorList)
for n in range(numColors):
dist = math.sqrt(((abs(self.colorList[n][0] - rVal)) ** 2) +
((abs(self.colorList[n][1] - gVal)) ** 2) +
((abs(self.colorList[n][2] - bVal)) ** 2))
if dist <= minDist:
minDist = dist
colorOutIndex = n
if colorOutIndex == -1:
exit(49)
else:
return self.colorList[colorOutIndex]
# Sets a pixel's color to the member of self.colorList with the closest R value to the input color
def limitColorsByMatchR(self, rgbIn):
if not self.colorList:
exit(461)
# Stores the R value of the input color
rVal = rgbIn[0]
# Stores the distance of the color (within self.colors) with the closest R value to the input color
# Initialized as the difference between the maximum and minimum possible values of R
rMinDist = 255
# Stores the index (within self.colors) of the color which most closely matches the input color
colorOutIndex = -1
numColors = len(self.colorList)
for n in range(numColors):
rDist = abs(self.colorList[n][0] - rVal)
if rDist <= rMinDist:
rMinDist = rDist
colorOutIndex = n
if colorOutIndex == -1:
exit(46)
else:
return self.colorList[colorOutIndex]
# Sets a pixel's color to the member of self.colorList with the closest G value to the input color
def limitColorsByMatchG(self, rgbIn):
if not self.colorList:
exit(471)
# Stores the G value of the input color
gVal = rgbIn[1]
# Stores the distance of the color (within self.colors) with the closest G value to the input color
# Initialized as the difference between the maximum and minimum possible values of G
gMinDist = 255
# Stores the index (within self.colors) of the color which most closely matches the input color
colorOutIndex = -1
numColors = len(self.colorList)
for n in range(numColors):
gDist = abs(self.colorList[n][1] - gVal)
if gDist <= gMinDist:
gMinDist = gDist
colorOutIndex = n
if colorOutIndex == -1:
exit(47)
else:
return self.colorList[colorOutIndex]
# Sets a pixel's color to the member of self.colorList with the closest B value to the input color
def limitColorsByMatchB(self, rgbIn):
if not self.colorList:
exit(481)
# Stores the B value of the input color
bVal = rgbIn[2]
# Stores the distance of the color (within self.colors) with the closest B value to the input color
# Initialized as the difference between the maximum and minimum possible values of B
bMinDist = 255
# Stores the index (within self.colors) of the color which most closely matches the input color
colorOutIndex = -1
numColors = len(self.colorList)
for n in range(numColors):
bDist = abs(self.colorList[n][2] - bVal)
if bDist <= bMinDist:
bMinDist = bDist
colorOutIndex = n
if colorOutIndex == -1:
exit(48)
else:
return self.colorList[colorOutIndex]
# Sets a pixel's color to the member of self.colorList with the closest H value to the input color
def limitColorsByMatchH(self, rgbIn):
if not self.colorList:
exit(431)
if not self.colorListHSV:
self.generateColorListHSV()
# Stores the H value of the input color
hVal = self.fromRGBtoHSV(rgbIn)[0]
# Stores the distance of the color (within self.colors) with the closest H value to the input color
# Initialized as the difference between the maximum and minimum possible values of H
hMinDist = 360
# Stores the index (within self.colors) of the color which most closely matches the input color
colorOutIndex = -1
numColors = len(self.colorList)
for n in range(numColors):
hDist = abs(self.colorListHSV[n][0] - hVal)
if hDist <= hMinDist:
hMinDist = hDist
colorOutIndex = n
if colorOutIndex == -1:
exit(43)
else:
return self.colorList[colorOutIndex]
# Sets a pixel's color to the member of self.colorList with the closest S value to the input color
def limitColorsByMatchS(self, rgbIn):
if not self.colorList:
exit(441)
if not self.colorListHSV:
self.generateColorListHSV()
# Stores the S value of the input color
sVal = self.fromRGBtoHSV(rgbIn)[1]
# Stores the distance of the color (within self.colors) with the closest S value to the input color
# Initialized as the difference between the maximum and minimum possible values of S
sMinDist = 1.0
# Stores the index (within self.colors) of the color which most closely matches the input color
colorOutIndex = -1
numColors = len(self.colorList)
for n in range(numColors):
sDist = abs(self.colorListHSV[n][1] - sVal)
if sDist <= sMinDist:
sMinDist = sDist
colorOutIndex = n
if colorOutIndex == -1: