source: subversion/applications/routing/pyroute-dev/colorsys.py @ 34690

Last change on this file since 34690 was 18455, checked in by buerste, 10 years ago

-remove BOM
-fix version strings

  • Property svn:keywords set to Rev
File size: 3.3 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""Conversion functions between RGB and other color systems.
4
5This modules provides two functions for each color system ABC:
6
7        rgb_to_abc(r, g, b) --> a, b, c
8        abc_to_rgb(a, b, c) --> r, g, b
9
10All inputs and outputs are triples of floats in the range [0.0...1.0]
11(with the exception of I and Q, which covers a slightly larger range).
12Inputs outside the valid range may cause exceptions or invalid outputs.
13
14Supported color systems:
15RGB: Red, Green, Blue components
16YIQ: Luminance, Chrominance (used by composite video signals)
17HLS: Hue, Luminance, Saturation
18HSV: Hue, Saturation, Value
19"""
20
21# References:
22# http://en.wikipedia.org/wiki/YIQ
23# http://en.wikipedia.org/wiki/HLS_color_space
24# http://en.wikipedia.org/wiki/HSV_color_space
25
26__all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
27                         "rgb_to_hsv","hsv_to_rgb"]
28
29# Some floating point constants
30
31ONE_THIRD = 1.0/3.0
32ONE_SIXTH = 1.0/6.0
33TWO_THIRD = 2.0/3.0
34
35# YIQ: used by composite video signals (linear combinations of RGB)
36# Y: perceived grey level (0.0 == black, 1.0 == white)
37# I, Q: color components
38
39def rgb_to_yiq(r, g, b):
40        y = 0.30*r + 0.59*g + 0.11*b
41        i = 0.60*r - 0.28*g - 0.32*b
42        q = 0.21*r - 0.52*g + 0.31*b
43        return (y, i, q)
44
45def yiq_to_rgb(y, i, q):
46        r = y + 0.948262*i + 0.624013*q
47        g = y - 0.276066*i - 0.639810*q
48        b = y - 1.105450*i + 1.729860*q
49        if r < 0.0: r = 0.0
50        if g < 0.0: g = 0.0
51        if b < 0.0: b = 0.0
52        if r > 1.0: r = 1.0
53        if g > 1.0: g = 1.0
54        if b > 1.0: b = 1.0
55        return (r, g, b)
56
57
58# HLS: Hue, Luminance, Saturation
59# H: position in the spectrum
60# L: color lightness
61# S: color saturation
62
63def rgb_to_hls(r, g, b):
64        maxc = max(r, g, b)
65        minc = min(r, g, b)
66        # XXX Can optimize (maxc+minc) and (maxc-minc)
67        l = (minc+maxc)/2.0
68        if minc == maxc: return 0.0, l, 0.0
69        if l <= 0.5: s = (maxc-minc) / (maxc+minc)
70        else: s = (maxc-minc) / (2.0-maxc-minc)
71        rc = (maxc-r) / (maxc-minc)
72        gc = (maxc-g) / (maxc-minc)
73        bc = (maxc-b) / (maxc-minc)
74        if r == maxc: h = bc-gc
75        elif g == maxc: h = 2.0+rc-bc
76        else: h = 4.0+gc-rc
77        h = (h/6.0) % 1.0
78        return h, l, s
79
80def hls_to_rgb(h, l, s):
81        if s == 0.0: return l, l, l
82        if l <= 0.5: m2 = l * (1.0+s)
83        else: m2 = l+s-(l*s)
84        m1 = 2.0*l - m2
85        return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
86
87def _v(m1, m2, hue):
88        hue = hue % 1.0
89        if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0
90        if hue < 0.5: return m2
91        if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
92        return m1
93
94
95# HSV: Hue, Saturation, Value
96# H: position in the spectrum
97# S: color saturation ("purity")
98# V: color brightness
99
100def rgb_to_hsv(r, g, b):
101        maxc = max(r, g, b)
102        minc = min(r, g, b)
103        v = maxc
104        if minc == maxc: return 0.0, 0.0, v
105        s = (maxc-minc) / maxc
106        rc = (maxc-r) / (maxc-minc)
107        gc = (maxc-g) / (maxc-minc)
108        bc = (maxc-b) / (maxc-minc)
109        if r == maxc: h = bc-gc
110        elif g == maxc: h = 2.0+rc-bc
111        else: h = 4.0+gc-rc
112        h = (h/6.0) % 1.0
113        return h, s, v
114
115def hsv_to_rgb(h, s, v):
116        if s == 0.0: return v, v, v
117        i = int(h*6.0) # XXX assume int() truncates!
118        f = (h*6.0) - i
119        p = v*(1.0 - s)
120        q = v*(1.0 - s*f)
121        t = v*(1.0 - s*(1.0-f))
122        if i%6 == 0: return v, t, p
123        if i == 1: return q, v, p
124        if i == 2: return p, v, t
125        if i == 3: return p, q, v
126        if i == 4: return t, p, v
127        if i == 5: return v, p, q
128        # Cannot get here
Note: See TracBrowser for help on using the repository browser.