├── .travis.yml ├── AUTHORS.md ├── LICENSE ├── README.md ├── appveyor.yml ├── pyrgb ├── __init__.py ├── __main__.py ├── rgb_function1.py ├── rgb_function2.py └── test.py └── setup.py /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: true 2 | 3 | language: python 4 | 5 | python: 6 | - 3.5 7 | 8 | install: 9 | - pip3 install codecov 10 | - python3 setup.py install 11 | 12 | script: 13 | - python3 -m pyrgb 14 | after_success: 15 | - codecov -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | # Core Developers # 2 | 3 | ---------- 4 | - Sepand Haghighi - Sharif University of Technology - ([http://github.com/sepandhaghighi](http://github.com/sepandhaghighi)) ([sepand@qpage.ir](mailto:sepand@qpage.ir)) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Moduland Co 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 |
5 | PyPI version 6 | 7 | 8 | DOI 9 | 10 | Codecov 11 | 12 | built with Python3 13 |
14 | 15 | 16 | ---------- 17 | 18 | 19 | # pyrgb 20 | Python RGB Conversion Library 21 | 22 | 23 | ## Installation 24 | ### Source Code 25 | - Download [Version 0.1](https://github.com/moduland/pyrgb/archive/v0.1.zip) or [Latest Source ](https://github.com/Moduland/pyrgb/archive/master.zip) 26 | 27 | - `python3 setup.py install` or `python setup.py install` (Need Root Access) 28 | 29 | ### PyPI 30 | 31 | - Check [Python Packaging User Guide](https://packaging.python.org/installing/) 32 | - `pip3 install pyrgb` or `pip install pyrgb` (Need Root Access) 33 | 34 | 35 | ## Functions 36 | 37 | ```python 38 | import pyrgb 39 | 40 | #1- hsv_rgb 41 | 42 | [R,G,B]=pyrgb.hsv_rgb(H,S,V) 43 | 44 | #2- hsl_rgb 45 | 46 | [R,G,B]=pyrgb.hsl_rgb(H,S,L) 47 | 48 | #3- hex_rgb 49 | 50 | [R,G,B]=pyrgb.hex_rgb('hex_string') 51 | 52 | #4- cmyk_rgb 53 | 54 | [R,G,B]=pyrgb.cmyk_rgb(C,M,Y,K) 55 | 56 | #5- rgb_hex 57 | 58 | hex_string=pyrgb.rgb_hex(R,G,B) 59 | 60 | #6- rgb_hsv 61 | 62 | [H,S,V]=pyrgb.rgb_hsv(R,G,B) 63 | 64 | #7- rgb_hsl 65 | 66 | [H,S,L]=pyrgb.rgb_hsl(R,G,B) 67 | 68 | #8- rgb_cmyk 69 | 70 | [C,M,Y,K]=pyrgb.rgb_cmyk(R,G,B) 71 | 72 | 73 | 74 | ``` 75 | 76 | - H: Hue (0<=H<360) 77 | - S: Saturation (0 84 | 85 | 86 | 87 | 88 | ## Automated Build 89 | 90 |
91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 |
LinuxWindows
105 | 106 |
107 | 108 | 109 | ## Issues & Bug Reports 110 | 111 | Just fill an issue and describe it. We'll check it ASAP! 112 | or send an email to [info@moduland.ir](mailto:info@moduland.ir "info@moduland.ir"). 113 | 114 | 115 | ## Contribution 116 | 117 | You can fork the repository, improve or fix some part of it and then send the pull requests back if you want to see them here. I really appreciate that. ❤️ 118 | 119 | Remember to write a few tests for your code before sending pull requests. 120 | 121 | 122 | ## Donate to our project 123 | 124 | If you feel like our project is important can you please support us? 125 | Our project is not and is never going to be working for profit. We need the money just so we can continue doing what we do. 126 | 127 |

Bitcoin :

128 | 129 | ```1XGr9qbZjBpUQJJSB6WtgBQbDTgrhPLPA``` 130 | 131 | 132 |

Payping (For Iranian citizens) :

133 | 134 | 135 | 136 | 137 | ## Citing 138 | If you use pyrgb in your research , please cite this ;-) 139 | 140 |
141 |

Sepand Haghighi. (2017, July 28). Moduland/pyrgb: Version 0.1. Zenodo. http://doi.org/10.5281/zenodo.835824

142 |
143 | 144 | 145 | ## License 146 |
147 | 148 |
149 | Moduland Website 150 | 151 |
152 | 153 | 154 | 155 | 156 | 157 | 158 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | build: false 2 | 3 | environment: 4 | matrix: 5 | 6 | - PYTHON: "C:\\Python35" 7 | PYTHON_VERSION: "3.5.2" 8 | PYTHON_ARCH: "32" 9 | 10 | - PYTHON: "C:\\Python35" 11 | PYTHON_VERSION: "3.5.2" 12 | PYTHON_ARCH: "64" 13 | 14 | 15 | 16 | init: 17 | - "ECHO %PYTHON% %PYTHON_VERSION% %PYTHON_ARCH%" 18 | 19 | install: 20 | - "%PYTHON%/python.exe setup.py install" 21 | 22 | test_script: 23 | - "%PYTHON%/python.exe -m pyrgb" -------------------------------------------------------------------------------- /pyrgb/__init__.py: -------------------------------------------------------------------------------- 1 | from .rgb_function1 import * 2 | from .rgb_function2 import * -------------------------------------------------------------------------------- /pyrgb/__main__.py: -------------------------------------------------------------------------------- 1 | from .rgb_function1 import * 2 | from .rgb_function2 import * 3 | import doctest 4 | if __name__=="__main__": 5 | doctest.testfile("test.py", verbose=True) 6 | -------------------------------------------------------------------------------- /pyrgb/rgb_function1.py: -------------------------------------------------------------------------------- 1 | import doctest 2 | from math import ceil 3 | def hsv_rgb(H,S,V): 4 | ''' 5 | This function Convert HSV to RGB 6 | :param H: Hue (0<=H<360) 7 | :type H:int 8 | :param S: Saturation (0>> hsv_rgb(240,1,0.5) 14 | [0, 0, 128] 15 | >>> hsv_rgb(60,1,1) 16 | [255, 255, 0] 17 | ''' 18 | try: 19 | C=V*S 20 | X=C*(1-abs(((H/60)%2)-1)) 21 | m=V-C 22 | 23 | if H>=0 and H<60: 24 | R_prime=C 25 | G_prime=X 26 | B_prime=0 27 | elif H>=60 and H<120: 28 | R_prime = X 29 | G_prime = C 30 | B_prime = 0 31 | elif H>=120 and H<180: 32 | R_prime = 0 33 | G_prime = C 34 | B_prime = X 35 | elif H>=180 and H<240: 36 | R_prime = 0 37 | G_prime = X 38 | B_prime = C 39 | elif H>=240 and H<300: 40 | R_prime = X 41 | G_prime = 0 42 | B_prime = C 43 | else: 44 | R_prime = C 45 | G_prime = 0 46 | B_prime = X 47 | R=ceil((R_prime+m)*255) 48 | G=ceil((G_prime+m)*255) 49 | B=ceil((B_prime+m)*255) 50 | return [R,G,B] 51 | except Exception as e: 52 | print("Input Error") 53 | 54 | 55 | def hsl_rgb(H,S,L): 56 | ''' 57 | This function convert hsl to rgb 58 | :param H: Hue (0<=H<360) 59 | :type H:int 60 | :param S: Saturation (0>> hsl_rgb(240,1,0.25) 66 | [0, 0, 128] 67 | >>> hsl_rgb(0,0,0.75) 68 | [192, 192, 192] 69 | ''' 70 | try: 71 | C=(1-abs(2*L-1))*S 72 | X=C*(1-abs(((H/60)%2)-1)) 73 | m=L-(C/2) 74 | 75 | if H>=0 and H<60: 76 | R_prime=C 77 | G_prime=X 78 | B_prime=0 79 | elif H>=60 and H<120: 80 | R_prime = X 81 | G_prime = C 82 | B_prime = 0 83 | elif H>=120 and H<180: 84 | R_prime = 0 85 | G_prime = C 86 | B_prime = X 87 | elif H>=180 and H<240: 88 | R_prime = 0 89 | G_prime = X 90 | B_prime = C 91 | elif H>=240 and H<300: 92 | R_prime = X 93 | G_prime = 0 94 | B_prime = C 95 | else: 96 | R_prime = C 97 | G_prime = 0 98 | B_prime = X 99 | R=ceil((R_prime+m)*255) 100 | G=ceil((G_prime+m)*255) 101 | B=ceil((B_prime+m)*255) 102 | 103 | return [R,G,B] 104 | except Exception as e: 105 | print("Input Error") 106 | 107 | def hex_rgb(hex_code): 108 | ''' 109 | This function convert hex_code to RGB 110 | :param hex_code: hex_code input 111 | :type hex_code:str 112 | :return: RGB as list [R,G,B] 113 | >>> hex_rgb('00FF00') 114 | [0, 255, 0] 115 | >>> hex_rgb('808080') 116 | [128, 128, 128] 117 | ''' 118 | try: 119 | R=int(hex_code[0:2],16) 120 | G=int(hex_code[2:4],16) 121 | B=int(hex_code[4:6],16) 122 | return [R,G,B] 123 | except Exception as e: 124 | print("Input Error") 125 | 126 | def cmyk_rgb(C,M,Y,K): 127 | ''' 128 | This function convert CMYK to RGB 129 | :param C: Cyan [0,1] 130 | :type C:float 131 | :param M: Magenta [0,1] 132 | :type M:float 133 | :param Y: Yellow [0,1] 134 | :type Y:float 135 | :param K: Black Key Color [0,1] 136 | :type K:float 137 | :return: RGB as list [R,G,B] 138 | >>> cmyk_rgb(0,0,0,1) 139 | [0, 0, 0] 140 | >>> cmyk_rgb(1,0,0,0) 141 | [0, 255, 255] 142 | ''' 143 | try: 144 | R=255*(1-C)*(1-K) 145 | G=255*(1-M)*(1-K) 146 | B=255*(1-Y)*(1-K) 147 | return [R,G,B] 148 | except Exception as e: 149 | print("Input Error") 150 | 151 | if __name__=="__main__": 152 | doctest.testmod() 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /pyrgb/rgb_function2.py: -------------------------------------------------------------------------------- 1 | import doctest 2 | def zero_insert(i): 3 | ''' 4 | This function insert zero to input string 5 | :param i: input string 6 | :type i:int 7 | :return: modified string 8 | 9 | >>> zero_insert("2") 10 | '02' 11 | >>> zero_insert("02") 12 | '02' 13 | ''' 14 | if len(i)==1: 15 | return "0"+i 16 | else: 17 | return i 18 | def rgb_hex(R,G,B): 19 | ''' 20 | This function convert RGB to HEX Format 21 | :param R: Red Color Code (0-255) 22 | :type R:int 23 | :param G: Green Color Code (0-255) 24 | :type G:int 25 | :param B: Blue Color Code (0-255) 26 | :type B:int 27 | :return: hex code as string 28 | >>> rgb_hex(0,0,0) 29 | '000000' 30 | >>> rgb_hex(192,192,192) 31 | 'C0C0C0' 32 | >>> rgb_hex(0,128,0) 33 | '008000' 34 | ''' 35 | try: 36 | R=zero_insert(str(hex(R)).replace("0x","")) 37 | G = zero_insert(str(hex(G)).replace("0x","")) 38 | B = zero_insert(str(hex(B)).replace("0x","")) 39 | response=(R+G+B).upper() 40 | return response 41 | except Exception as e: 42 | print("Input Error") 43 | 44 | def rgb_hsv(R,G,B): 45 | ''' 46 | This function convert RGB to HSV 47 | :param R: Red Color Code (0-255) 48 | :type R:int 49 | :param G: Green Color Code (0-255) 50 | :type G:int 51 | :param B: Blue Color Code (0-255) 52 | :type B:int 53 | :return: HSV as list [H,S,V] 54 | >>> rgb_hsv(255,0,0) 55 | [0, 1.0, 1.0] 56 | >>> rgb_hsv(128,128,0) 57 | [60, 1.0, 0.5] 58 | ''' 59 | try: 60 | R_prime=R/255 61 | G_prime=G/255 62 | B_prime=B/255 63 | C_max=max(R_prime,G_prime,B_prime) 64 | C_min=min(R_prime,G_prime,B_prime) 65 | delta=C_max-C_min 66 | if delta==0: 67 | H=0 68 | elif C_max==R_prime: 69 | H=60*(((G_prime-B_prime)/delta)%6) 70 | elif C_max==G_prime: 71 | H = 60 * (((B_prime - R_prime) / delta) +2) 72 | else: 73 | H = 60 * (((R_prime - G_prime) / delta) + 4) 74 | H=round(H) 75 | if C_max==0: 76 | S=0 77 | else: 78 | S=round(delta/C_max,2) 79 | V=round(C_max,2) 80 | return [H,S,V] 81 | except Exception as e: 82 | print("Input Error") 83 | 84 | 85 | def rgb_hsl(R,G,B): 86 | ''' 87 | This function convert rgb to hsl 88 | :param R: Red Color Code (0-255) 89 | :type R:int 90 | :param G: Green Color Code (0-255) 91 | :type G:int 92 | :param B: Blue Color Code (0-255) 93 | :type B:int 94 | :return: HSL as list [H,S,L] 95 | >>> rgb_hsl(128,0,0) 96 | [0, 1.0, 0.25] 97 | >>> rgb_hsl(255,255,0) 98 | [60, 1.0, 0.5] 99 | 100 | ''' 101 | try: 102 | R_prime=R/255 103 | G_prime=G/255 104 | B_prime=B/255 105 | C_max=max(R_prime,G_prime,B_prime) 106 | C_min=min(R_prime,G_prime,B_prime) 107 | delta=C_max-C_min 108 | if delta==0: 109 | H=0 110 | elif C_max==R_prime: 111 | H=60*(((G_prime-B_prime)/delta)%6) 112 | elif C_max==G_prime: 113 | H = 60 * (((B_prime - R_prime) / delta) +2) 114 | else: 115 | H = 60 * (((R_prime - G_prime) / delta) + 4) 116 | H=round(H) 117 | L = round((C_max+C_min)/2,2) 118 | 119 | if C_max==0: 120 | S=0 121 | else: 122 | S=round(delta/(1-abs(2*L-1)),2) 123 | return [H,S,L] 124 | except Exception as e: 125 | print("Input Error") 126 | 127 | 128 | def rgb_cmyk(R,G,B): 129 | ''' 130 | This function convert RGB to CMYK 131 | :param R: Red Color Code (0-255) 132 | :type R:int 133 | :param G: Green Color Code (0-255) 134 | :type G:int 135 | :param B: Blue Color Code (0-255) 136 | :type B:int 137 | :return: CMYK as list [C,M,Y,K] 138 | >>> rgb_cmyk(0,0,255) 139 | [1.0, 1.0, 0.0, 0.0] 140 | >>> rgb_cmyk(0,255,255) 141 | [1.0, 0.0, 0.0, 0.0] 142 | ''' 143 | try: 144 | R_prime=R/255 145 | G_prime=G/255 146 | B_prime=B/255 147 | K=round(1-max(R_prime,G_prime,B_prime),2) 148 | C=round((1-R_prime-K)/(1-K),2) 149 | M=round((1-G_prime-K)/(1-K),2) 150 | Y=round((1-B_prime-K)/(1-K),2) 151 | return [C,M,Y,K] 152 | except Exception as e: 153 | print("Input Error") 154 | 155 | if __name__=="__main__": 156 | doctest.testmod() 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /pyrgb/test.py: -------------------------------------------------------------------------------- 1 | ''' 2 | >>> from pyrgb import * 3 | >>> import coverage 4 | >>> cov = coverage.Coverage() 5 | >>> cov.start() 6 | >>> hsv_rgb(240,1,0.5) 7 | [0, 0, 128] 8 | >>> hsv_rgb(50,1,0.5) 9 | [128, 107, 0] 10 | >>> hsv_rgb(60,1,1) 11 | [255, 255, 0] 12 | >>> hsv_rgb(120,1,1) 13 | [0, 255, 0] 14 | >>> hsv_rgb(180,1,1) 15 | [0, 255, 255] 16 | >>> hsv_rgb(330,1,1) 17 | [255, 0, 128] 18 | >>> hsl_rgb(240,1,0.25) 19 | [0, 0, 128] 20 | >>> hsl_rgb(0,0,0.75) 21 | [192, 192, 192] 22 | >>> hsl_rgb(60,1,0.25) 23 | [128, 128, 0] 24 | >>> hsl_rgb(120,1,0.25) 25 | [0, 128, 0] 26 | >>> hsl_rgb(180,1,0.25) 27 | [0, 128, 128] 28 | >>> hsl_rgb(330,1,0.25) 29 | [128, 0, 64] 30 | >>> hex_rgb('00FF00') 31 | [0, 255, 0] 32 | >>> hex_rgb('808080') 33 | [128, 128, 128] 34 | >>> cmyk_rgb(0,0,0,1) 35 | [0, 0, 0] 36 | >>> cmyk_rgb(1,0,0,0) 37 | [0, 255, 255] 38 | >>> zero_insert("2") 39 | '02' 40 | >>> zero_insert("02") 41 | '02' 42 | >>> rgb_hex(0,0,0) 43 | '000000' 44 | >>> rgb_hex(192,192,192) 45 | 'C0C0C0' 46 | >>> rgb_hex(0,128,0) 47 | '008000' 48 | >>> rgb_hsv(255,0,0) 49 | [0, 1.0, 1.0] 50 | >>> rgb_hsv(0,255,0) 51 | [120, 1.0, 1.0] 52 | >>> rgb_hsv(0,0,250) 53 | [240, 1.0, 0.98] 54 | >>> rgb_hsv(0,0,0) 55 | [0, 0, 0.0] 56 | >>> rgb_hsv(128,128,0) 57 | [60, 1.0, 0.5] 58 | >>> rgb_hsl(128,0,0) 59 | [0, 1.0, 0.25] 60 | >>> rgb_hsl(0,128,0) 61 | [120, 1.0, 0.25] 62 | >>> rgb_hsl(0,0,128) 63 | [240, 1.0, 0.25] 64 | >>> rgb_hsl(0,0,0) 65 | [0, 0, 0.0] 66 | >>> rgb_hsl(255,255,0) 67 | [60, 1.0, 0.5] 68 | >>> rgb_cmyk(0,0,255) 69 | [1.0, 1.0, 0.0, 0.0] 70 | >>> rgb_cmyk(0,255,255) 71 | [1.0, 0.0, 0.0, 0.0] 72 | >>> cov.stop() 73 | >>> cov.save() 74 | 75 | ''' -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | setup( 3 | name = 'pyrgb', 4 | packages = ['pyrgb'], 5 | version = '0.1', 6 | description = 'RGB Conversion Functions', 7 | long_description="", 8 | author = 'Moduland Co', 9 | author_email = 'info@moduland.ir', 10 | url = 'https://github.com/Moduland/pyrgb', 11 | download_url = 'https://github.com/Moduland/pyrgb/tarball/v0.1', 12 | keywords = ['RGB', 'HSV', 'HSL','python','CMYK'], 13 | classifiers = [], 14 | license='MIT', 15 | ) 16 | --------------------------------------------------------------------------------