File: color.py

package info (click to toggle)
python-vispy 0.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,240 kB
  • sloc: python: 57,407; javascript: 6,810; makefile: 63; sh: 5
file content (70 lines) | stat: -rw-r--r-- 1,695 bytes parent folder | download | duplicates (3)
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
"""Color-related GLSL functions."""


# -----------------------------------------------------------------------------
# Colormaps
# -----------------------------------------------------------------------------

"""Texture lookup for a discrete color map stored in a 1*ncolors 2D texture.

The `get_color()` function returns a RGB color from an index integer
referring to the colormap.


Inputs
------

index (int): The color index.


Template variables
------------------

$ncolors (int): The number of colors in the colormap.

$colormap (2D texture sampler): The sampler for the 2D 1*ncolors colormap
    texture.


Outputs
-------

color (vec3): The color.

"""
COLORMAP_TEXTURE = """
vec3 get_color(int index) {
    float x = (float(index) + .5) / float($ncolors);
    return texture2D($colormap, vec2(x, .5)).rgb;
}
"""


# -----------------------------------------------------------------------------
# Color space transformations
# -----------------------------------------------------------------------------

# From http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
# TODO: unit tests
HSV_TO_RGB = """
vec3 hsv_to_rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
"""


RGB_TO_HSV = """
vec3 rgb_to_hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
"""