-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_utils.py
More file actions
executable file
·87 lines (64 loc) · 2.47 KB
/
Copy pathcolor_utils.py
File metadata and controls
executable file
·87 lines (64 loc) · 2.47 KB
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
#!/usr/bin/env python
"""Helper functions to make color manipulations easier."""
from __future__ import division
import math
def remap(x, oldmin, oldmax, newmin, newmax):
"""Remap the float x from the range oldmin-oldmax to the range newmin-newmax
Does not clamp values that exceed min or max.
For example, to make a sine wave that goes between 0 and 256:
remap(math.sin(time.time()), -1, 1, 0, 256)
"""
zero_to_one = (x-oldmin) / (oldmax-oldmin)
return zero_to_one*(newmax-newmin) + newmin
def clamp(x, minn, maxx):
"""Restrict the float x to the range minn-maxx."""
return max(minn, min(maxx, x))
def cos(x, offset=0, period=1, minn=0, maxx=1):
"""A cosine curve scaled to fit in a 0-1 range and 0-1 domain by default.
offset: how much to slide the curve across the domain (should be 0-1)
period: the length of one wave
minn, maxx: the output range
"""
value = math.cos((x/period - offset) * math.pi * 2) / 2 + 0.5
return value*(maxx-minn) + minn
def contrast(color, center, mult):
"""Expand the color values by a factor of mult around the pivot value of center.
color: an (r, g, b) tuple
center: a float -- the fixed point
mult: a float -- expand or contract the values around the center point
"""
r, g, b = color
r = (r - center) * mult + center
g = (g - center) * mult + center
b = (b - center) * mult + center
return (r, g, b)
def clip_black_by_luminance(color, threshold):
"""If the color's luminance is less than threshold, replace it with black.
color: an (r, g, b) tuple
threshold: a float
"""
r, g, b = color
if r+g+b < threshold*3:
return (0, 0, 0)
return (r, g, b)
def clip_black_by_channels(color, threshold):
"""Replace any individual r, g, or b value less than threshold with 0.
color: an (r, g, b) tuple
threshold: a float
"""
r, g, b = color
if r < threshold: r = 0
if g < threshold: g = 0
if b < threshold: b = 0
return (r, g, b)
def mod_dist(a, b, n):
"""Return the distance between floats a and b, modulo n.
The result is always non-negative.
For example, thinking of a clock:
mod_dist(11, 1, 12) == 2 because you can "wrap around".
"""
return min((a-b) % n, (b-a) % n)
def gamma(color, gamma):
"""Apply a gamma curve to the color. The color values should be in the range 0-1."""
r, g, b = color
return (max(r, 0) ** gamma, max(g, 0) ** gamma, max(b, 0) ** gamma)