Skip to content

Commit

Permalink
Random mode (#124)
Browse files Browse the repository at this point in the history
* feat : random mode added to Projection #88

* feat : random mode added to color and bgcolor #88

* fix : tests updated #88

* fix : tests updated #88

* fix : tests updated #88

* fix : autopep8 #88

* doc : CHANGELOG updated #88

* doc : README updated #88

* fix : tests updated #124
  • Loading branch information
sepandhaghighi authored Apr 26, 2022
1 parent 32c13ec commit 7444b9b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `random_hex_color_gen` function
- `color`,`bgcolor` and `projection` parameters random mode
### Changed
- Hex color support for `color` and `bgcolor` parameters
- Test system modified
- Random mode modified
- `filter_color` function modified
- `filter_projection` function modified
- `README.md` updated
## [0.6] - 2022-04-13
### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Samila is a generative art generator written in Python, Samila let's you create
```
<img src="https://github.com/sepandhaghighi/samila/raw/master/otherfiles/images/2.png">

* Supported projections : `RECTILINEAR`, `POLAR`, `AITOFF`, `HAMMER`, `LAMBERT` and `MOLLWEIDE`
* Supported projections : `RECTILINEAR`, `POLAR`, `AITOFF`, `HAMMER`, `LAMBERT`, `MOLLWEIDE` and `RANDOM`
* Default projection is `RECTILINEAR`

### Range
Expand Down Expand Up @@ -177,6 +177,7 @@ Samila is a generative art generator written in Python, Samila let's you create
1. Color name (example: `yellow`)
2. RGB/RGBA (example: `(0.1,0.1,0.1)`, `(0.1,0.1,0.1,0.1)`)
3. Hex (example: `#eeefff`)
4. Random (example: `random`)

### Regeneration
```pycon
Expand Down
5 changes: 3 additions & 2 deletions examples/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"## Projection\n",
"We can use the `projection` attribute to define the coordinate system to transform our functions\n",
"\n",
"The avaliable projections are `RECTILINEAR`, `POLAR`, `AITOFF`, `HAMMER`, `LAMBERT` and `MOLLWEIDE`"
"The avaliable projections are `RECTILINEAR`, `POLAR`, `AITOFF`, `HAMMER`, `LAMBERT`, `MOLLWEIDE` and `RANDOM`"
]
},
{
Expand Down Expand Up @@ -159,7 +159,8 @@
"\n",
" 1. Color name (example: `yellow`)\n",
" 2. RGB/RGBA (example: `(0.1,0.1,0.1)`, `(0.1,0.1,0.1,0.1)`)\n",
" 3. Hex (example: `#eeefff`)"
" 3. Hex (example: `#eeefff`)\n",
" 4. Random (example: `random`)"
]
},
{
Expand Down
19 changes: 18 additions & 1 deletion samila/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def random_equation_gen():
return result


def random_hex_color_gen():
"""
Generate random hex color code.
:return: color code as str
"""
random_color = "#%06x" % random.randint(0, 0xFFFFFF)
return random_color


def float_range(start, stop, step):
"""
Generate float range.
Expand Down Expand Up @@ -96,6 +106,8 @@ def filter_color(color):
if isinstance(color, tuple):
return color
if isinstance(color, str):
if color.upper() == "RANDOM":
return random_hex_color_gen()
if re.match(HEX_COLOR_PATTERN, color):
return color
distance_list = list(map(lambda x: distance_calc(color, x),
Expand All @@ -114,7 +126,12 @@ def filter_projection(projection):
:return: filtered version of projection
"""
if isinstance(projection, Projection):
return projection.value
projection_value = projection.value
if projection_value == "random":
projection_list = list(Projection)
projection_list.remove(Projection.RANDOM)
projection_value = random.choice(projection_list).value
return projection_value
return None


Expand Down
1 change: 1 addition & 0 deletions samila/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Projection(Enum):
LAMBERT = "lambert"
MOLLWEIDE = "mollweide"
RECTILINEAR = "rectilinear"
RANDOM = "random"


RANDOM_COEF_LIST = [
Expand Down
28 changes: 28 additions & 0 deletions test/function_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""
>>> import random
>>> from samila.functions import *
>>> s = list(float_range(1,1.5,0.1))
>>> s
Expand All @@ -12,12 +13,39 @@
'yellow'
>>> filter_color((0.2,0.3,0.4))
(0.2, 0.3, 0.4)
>>> filter_color("#FFFFFF")
'#FFFFFF'
>>> random.seed(2)
>>> color1 = filter_color("random")
>>> random.seed(3)
>>> color2 = filter_color("RANDOM")
>>> color1 == color2
False
>>> random.seed(2)
>>> color1 = random_hex_color_gen()
>>> random.seed(3)
>>> color2 = random_hex_color_gen()
>>> color1 == color2
False
>>> len(color1)
7
>>> len(color2)
7
>>> filter_color(2)
>>> filter_color(4)
>>> filter_size(2)
>>> filter_size((2, 'test'))
>>> filter_size((2, 3.5))
(2, 3.5)
>>> filter_projection(2)
>>> filter_projection(Projection.POLAR)
'polar'
>>> random.seed(2)
>>> projection1 = filter_projection(Projection.RANDOM)
>>> random.seed(3)
>>> projection2 = filter_projection(Projection.RANDOM)
>>> projection1 == projection2
False
>>> distance_calc("test","test1")
1
>>> distance_calc("te1st","test")
Expand Down
7 changes: 1 addition & 6 deletions test/nft_upload_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
"""
>>> import os
>>> import math
>>> import random
>>> import time
>>> from samila import GenerativeImage, Projection
>>> from samila.params import VALID_COLORS
Expand All @@ -11,10 +9,7 @@
>>> g.plot()
>>> NFT_STORAGE_API_KEY = os.environ["NFT_STORAGE_API_KEY"]
>>> g.generate()
>>> random_projection = random.choice(list(Projection))
>>> random_color = random.choice(VALID_COLORS)
>>> random_bgcolor = random.choice(VALID_COLORS)
>>> g.plot(projection=random_projection,color=random_color,bgcolor=random_bgcolor)
>>> g.plot(projection=Projection.RANDOM, color="random", bgcolor="random")
>>> counter = 0
>>> try_limit = 10
>>> status = False
Expand Down
12 changes: 12 additions & 0 deletions test/overall_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@
0.5
>>> g.linewidth
2.2
>>> random.seed(2)
>>> g.plot(color="random", bgcolor="random", projection=Projection.RANDOM)
>>> color1, bgcolor1, projection1 = g.color, g.bgcolor, g.projection
>>> random.seed(3)
>>> g.plot(color="random", bgcolor="random", projection=Projection.RANDOM)
>>> color2, bgcolor2, projection2 = g.color, g.bgcolor, g.projection
>>> color1 == color2
False
>>> bgcolor1 == bgcolor2
False
>>> projection1 == projection2
False
>>> result = g.nft_storage(api_key="")
>>> result['status']
False
Expand Down

0 comments on commit 7444b9b

Please sign in to comment.