File size: 1,580 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math

def angle_to_xy(angle: float, offset: float) -> (float, float):
    """Convert from radians to obfuscated (x, y) tupple.

    The `angle` must be in the range [0.0, 1.0].
    
    The `offset` must be in the range [0.0, 1.0].
    Provide an offset to obfuscate by rotating. offset=0 is no obfuscation.

    The returned values are in the range [0.0, 1.0].
    """
    
    radians = (angle + offset) % 1.0
    x = math.cos(radians * math.tau)
    y = math.sin(radians * math.tau)
    return (x, y)

def color_to_xy(color: int, color_count: int, offset: float) -> (float, float):
    """Convert from color value to obfuscated (x, y) tupple.

    The `color` must be in the range [0, color_count-1].
    
    The `offset` must be in the range [0.0, 1.0].
    Provide an offset to obfuscate. offset=0 is no obfuscation.

    The returned values are in the range [0.0, 1.0].
    """

    angle = color / float(color_count)
    return angle_to_xy(angle, offset)

def xy_to_color(xy: (float, float), color_count: int, offset: float) -> int:
    """Convert from obfuscated (x, y) tupple to deobfuscated color value.

    The `xy` tupple must be in the range [-1.0, 1.0].
    
    The `offset` must be in the range [0.0, 1.0].
    Provide an offset to deobfuscate. offset=0 is no deobfuscation.
    
    The `color_count` is the number of colors. typical 10.

    The returned value is in the range [0, color_count - 1].
    """
    
    (x, y) = xy
    angle = ((math.atan2(y, x) / math.tau) - offset) % 1.0
    value = math.floor(angle * color_count)
    return int(value)