Browse Source

Enhance color handling + add gray option

Yann Weber 6 years ago
parent
commit
bd862590b2
2 changed files with 39 additions and 11 deletions
  1. 7
    4
      gte/__main__.py
  2. 32
    7
      gte/world.py

+ 7
- 4
gte/__main__.py View File

@@ -73,7 +73,6 @@ parser_evolve.add_argument('--quiet', '-q', action='store_const',
73 73
                            default=False, const=True)
74 74
 
75 75
 
76
-
77 76
 parser_gen = subparsers.add_parser('generate', help='evolving help')
78 77
 
79 78
 parser_gen.add_argument('--prog', '-P', type=str, default=None)
@@ -86,6 +85,8 @@ parser_gen.add_argument('--turmit-count', '-T', type=int, metavar='N',
86 85
                         default=3)
87 86
 parser_gen.add_argument('--steps', '-s', type=int, metavar='N',
88 87
                         default=30000)
88
+parser_gen.add_argument('--gray', '-G', action='store_const',
89
+                        default=False, const=True)
89 90
 
90 91
 
91 92
 args = parser.parse_args()
@@ -93,8 +94,10 @@ args = parser.parse_args()
93 94
 if 'pool_size' in args:
94 95
     # Evolver
95 96
     if args.prog is None:
96
-        progs = [rpnlib.RpnExpr.random(args.prog_size)
97
-                for _ in range(args.pool_size)]
97
+        prog = rpnlib.RpnExpr.random(args.prog_size)
98
+        progs = [mutate(prog, force=True) for _ in range(args.pool_size-1)]
99
+        #progs = [rpnlib.RpnExpr.random(args.prog_size)
100
+        #        for _ in range(args.pool_size)]
98 101
     else:
99 102
         prog = rpnlib.RpnExpr.from_string(args.prog)
100 103
         progs = [mutate(prog, force=True) for _ in range(args.pool_size-1)]
@@ -170,7 +173,7 @@ if 'pool_size' in args:
170 173
     exit(0)
171 174
 else:
172 175
     # Generate
173
-    w = World(args.world_height, args.world_width)
176
+    w = World(args.world_height, args.world_width, gray=args.gray)
174 177
     prog = rpnlib.RpnExpr.from_string(args.prog)
175 178
     turmits = [LivingTurmit(world=w, prog=prog)
176 179
                for _ in range(args.turmit_count)]

+ 32
- 7
gte/world.py View File

@@ -16,7 +16,7 @@ def eval_prog(args):
16 16
         @param args : dirty argparser returned arguments
17 17
     '''
18 18
     generation, progid, prog, trynum, args = args
19
-    w = World(args.world_height, args.world_width)
19
+    w = World(args.world_height, args.world_width, gray=True)
20 20
     logger.debug('Running P%d run#%d %s' % (progid, trynum, prog))
21 21
     w.raz()
22 22
     turmits = [LivingTurmit(world=w, prog=prog)
@@ -44,7 +44,7 @@ def eval_prog(args):
44 44
 class World(object):
45 45
     ''' @brief A Turmit world. An array of dim == 2 with tuple(R,G,B) '''
46 46
     
47
-    def __init__(self, x, y):
47
+    def __init__(self, x, y, gray=False):
48 48
         if x < 1:
49 49
             raise ValueError('Invalid value %d for x size')
50 50
         if y < 1:
@@ -52,6 +52,7 @@ class World(object):
52 52
         self._x = x
53 53
         self._y = y
54 54
         self._val = None
55
+        self._gray = gray
55 56
         self.raz()
56 57
 
57 58
     @property
@@ -64,10 +65,13 @@ class World(object):
64 65
 
65 66
     def raz(self, color=None):
66 67
         ''' @brief Set all value to color
67
-            @param color tuple : tuple(R,G,B)
68
+            @param color tuple | float : tuple(R,G,B) or greylevel
68 69
         '''
69 70
         if color is None:
70
-            self._val = np.zeros([self._y, self._x, 3])
71
+            if self._gray:
72
+                self._val = np.zeros([self._y, self._x])
73
+            else:
74
+                self._val = np.zeros([self._y, self._x, 3])
71 75
         else:
72 76
             self._val = [[color for _ in range(self._x)] for _ in range(self._y)]
73 77
             self._val = np.array(self._val)
@@ -78,7 +82,10 @@ class World(object):
78 82
     
79 83
     def fractdim(self, threshold=None):
80 84
         ''' @return Minkowski–Bouligand dimension (computed) '''
81
-        Z = self.rgb2gray(self._val)
85
+        if not self._gray:
86
+            Z = self.rgb2gray(self._val)
87
+        else:
88
+            Z = self._val
82 89
 
83 90
         # Only for 2d image
84 91
         assert(len(Z.shape) == 2)
@@ -127,6 +134,12 @@ class World(object):
127 134
         gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
128 135
         return gray
129 136
 
137
+    @staticmethod
138
+    def single_rgb2gray(rgb):
139
+        r,g,b = rgb
140
+        gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
141
+        return gray
142
+
130 143
     def __getitem__(self, a):
131 144
         return self._val[a]
132 145
 
@@ -148,8 +161,17 @@ class LivingTurmit(Turmit):
148 161
             raise TypeError(msg % color)
149 162
         ## @brief Stores Turmit color
150 163
         rand_hue = randint(0,255) / 255
164
+        rand_lvl = randint(127,255) / 255
165
+        rand_val = randint(127,255) / 255
151 166
         self._col = tuple(round(i * 255)
152
-                          for i in colorsys.hsv_to_rgb(rand_hue,1,1))
167
+                          for i in colorsys.hsv_to_rgb(rand_hue,
168
+                                                       rand_lvl,
169
+                                                       rand_val))
170
+        if world._gray:
171
+            #self._col = 255.0
172
+            self._col = tuple(round(i * 255)
173
+                              for i in colorsys.hsv_to_rgb(rand_hue,1,1))
174
+            self._col = World.single_rgb2gray(self._col)
153 175
         #self._col = (randint(0,255), randint(0, 255), randint(0,255))
154 176
         if color is not None:
155 177
             self._col = color
@@ -170,7 +192,10 @@ class LivingTurmit(Turmit):
170 192
 
171 193
     def __call__(self):
172 194
         ''' @brief Exec a turmit and move it '''
173
-        r,g,b = self._world[self._y][self._x]
195
+        if self._world._gray:
196
+            r = g = b = self._world[self._y][self._x]
197
+        else:
198
+            r,g,b = self._world[self._y][self._x]
174 199
         tdir = super().__call__(x=self._x, y=self._y, r=r, g=g, b=b)
175 200
         self.move(tdir)
176 201
 

Loading…
Cancel
Save