├── .gitattributes ├── LICENSE.md ├── README.md ├── .gitignore └── luacolors.lua /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT LICENSE 2 | 3 | Copyright (c) 2014 Phoenix Enero 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a 6 | copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included 14 | in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # luacolors - a color utility library 2 | 3 | __luacolors__ is a collection of functions for common color conversions. 4 | 5 | Installation 6 | ------------ 7 | 8 | The luacolors.lua file should be dropped into an existing project and required by it. 9 | 10 | ```lua 11 | luacolors = require "luacolors" 12 | ```` 13 | 14 | Function Reference 15 | ------------------ 16 | 17 | ###luacolors.sRGBtoXYZ(r, g, b) 18 | 19 | Returns the CIEXYZ coordinates `X, Y, Z` for a given sRGB. 20 | 21 | ###luacolors.XYZtosRGB(X, Y, Z) 22 | 23 | Returns the gamma-corrected RGB values `r, g, b` for a given CIEXYZ. 24 | 25 | ###luacolors.XYZtoLab(X, Y, Z) 26 | 27 | Returns the CIELAB values `L, a, b` for a given CIEXYZ. 28 | 29 | ###luacolors.LabtoXYZ(L, a, b) 30 | 31 | Returns the CIEXYZ values `X, Y, Z` for a given CIELAB. 32 | 33 | ###luacolors.LabtosRGB(L, a, b) 34 | 35 | Returns the gamma-corrected RGB values `r, g, b` for a given CIELAB. 36 | 37 | ###luacolors.sRGBtoLab(r, g, b) 38 | 39 | Returns the CIELAB values `L, a, b` for a given sRGB. 40 | 41 | ###luacolors.HSLtoRGB(h, s, l) 42 | 43 | Returns the RGB values for a given HSL. 44 | 45 | ###luacolors.RGBtoHSL(r, g, b) 46 | 47 | Returns the HSL values for a given RGB. 48 | 49 | Color reference 50 | --------------- 51 | 52 | Any color included in http://en.wikipedia.org/wiki/CSS_colors . 53 | 54 | Example: 55 | ```lua 56 | luacolors.maroon -- returns {127, 0, 0} 57 | ```` 58 | 59 | Credits 60 | ------- 61 | 62 | Several wikipedia pages: 63 | http://en.wikipedia.org/wiki/SRGB 64 | http://en.wikipedia.org/wiki/Lab_color_space 65 | http://en.wikipedia.org/wiki/CIE_1931_color_space 66 | http://en.wikipedia.org/wiki/HSL_and_HSV 67 | http://en.wikipedia.org/wiki/CSS_colors 68 | 69 | Other: 70 | http://www.easyrgb.com/index.php?X=CALC for validating 71 | 72 | License 73 | ------- 74 | 75 | MIT LICENSE 76 | 77 | Copyright (c) 2014 Phoenix Enero 78 | 79 | Permission is hereby granted, free of charge, to any person obtaining a 80 | copy of this software and associated documentation files (the 81 | "Software"), to deal in the Software without restriction, including 82 | without limitation the rights to use, copy, modify, merge, publish, 83 | distribute, sublicense, and/or sell copies of the Software, and to 84 | permit persons to whom the Software is furnished to do so, subject to 85 | the following conditions: 86 | 87 | The above copyright notice and this permission notice shall be included 88 | in all copies or substantial portions of the Software. 89 | 90 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 91 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 92 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 93 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 94 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 95 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 96 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /luacolors.lua: -------------------------------------------------------------------------------- 1 | local luacolors = { 2 | _VERSION = 'luacolors v0.1.0', 3 | _DESCRIPTION = 'Color utility library for Lua', 4 | _URL = 'https://github.com/icrawler/luacolors', 5 | _LICENSE = [[ 6 | MIT LICENSE 7 | 8 | Copyright (c) 2014 Phoenix Enero 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a 11 | copy of this software and associated documentation files (the 12 | "Software"), to deal in the Software without restriction, including 13 | without limitation the rights to use, copy, modify, merge, publish, 14 | distribute, sublicense, and/or sell copies of the Software, and to 15 | permit persons to whom the Software is furnished to do so, subject to 16 | the following conditions: 17 | 18 | The above copyright notice and this permission notice shall be included 19 | in all copies or substantial portions of the Software. 20 | 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 22 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 24 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 25 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 26 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 27 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | ]] 29 | } 30 | 31 | -- private methods 32 | local function f1(t) 33 | return t > 0.0088564516790356 and t^0.3333333333333333 or 34 | 0.3333333333333333*23.3611111111111111*t + 0.1379310344827586 35 | end 36 | 37 | local function f2(t) 38 | return t > 0.2068965517241379 and t*t*t or 39 | 3*0.0428061831153388*(t-0.1379310344827586) 40 | end 41 | 42 | -- public functions 43 | 44 | -- Convert gamma-corrected RGB to CIEXYZ 45 | local function sRGBtoXYZ(r, g, b) 46 | local f = 1/255 47 | r = r*f 48 | g = g*f 49 | b = b*f 50 | r = r <= 0.04045 and r/12.92 or ((r+0.055)/(1.055))^2.4 51 | g = g <= 0.04045 and g/12.92 or ((g+0.055)/(1.055))^2.4 52 | b = b <= 0.04045 and b/12.92 or ((b+0.055)/(1.055))^2.4 53 | local X = 0.4124*r + 0.3576*g + 0.1805*b 54 | local Y = 0.2126*r + 0.7152*g + 0.0722*b 55 | local Z = 0.0193*r + 0.1192*g + 0.9502*b 56 | return X*100, Y*100, Z*100 57 | end 58 | 59 | 60 | -- Convert CIEXYZ to gamma-corrected RGB 61 | local function XYZtosRGB(X, Y, Z) 62 | X, Y, Z = X/100, Y/100, Z/100 63 | local r = 3.2406*X - 1.5372*Y - 0.4986*Z 64 | local g = -0.9689*X + 1.8758*Y + 0.0415*Z 65 | local b = 0.0557*X - 0.2040*Y + 1.0570*Z 66 | local k = 1/2.4 67 | local floor = math.floor 68 | r = floor((r <= 0.0031308 and 12.92*r or 1.055*r^k-0.055)*255+0.5) 69 | g = floor((g <= 0.0031308 and 12.92*g or 1.055*g^k-0.055)*255+0.5) 70 | b = floor((b <= 0.0031308 and 12.92*b or 1.055*b^k-0.055)*255+0.5) 71 | return r, g, b 72 | end 73 | 74 | -- convert CIEXYZ to CIELAB 75 | local function XYZtoLab(X, Y, Z) 76 | local yon = f1(Y/100) 77 | local L = 116*(yon) - 16 78 | local a = 500*(f1(X/95.047) - yon) 79 | local b = 200*(yon - f1(Z/108.883)) 80 | return L, a, b 81 | end 82 | 83 | 84 | -- convert gamma-corrected RGB to CIELAB 85 | local function sRGBtoLab(r, g, b) 86 | return XYZtoLab(sRGBtoXYZ(r, g, b)) 87 | end 88 | 89 | -- convert CIELAB to CIEXYZ 90 | local function LabtoXYZ(L, a, b) 91 | local k = 0.0086206896551724*(L+16) 92 | local Y = 100*f2(k) 93 | local X = 95.047*f2(k+0.002*a) 94 | local Z = 108.883*f2(k-0.005*b) 95 | return X, Y, Z 96 | end 97 | 98 | 99 | -- convert CIELAB to gamma-corrected RGB 100 | local function LabtosRGB(L, a, b) 101 | return XYZtosRGB(LabtoXYZ(L, a, b)) 102 | end 103 | 104 | 105 | -- convert HSL to RGB 106 | -- (taken from https://www.love2d.org/wiki/HSL_color with a few modifications) 107 | local function HSLtoRGB(h, s, l) 108 | if s<=0 then return l,l,l end 109 | h, s, l = h/360*6, s/255, l/255 110 | local c = (1-math.abs(2*l-1))*s 111 | local x = (1-math.abs(h%2-1))*c 112 | local m,r,g,b = (l-.5*c), 0,0,0 113 | if h < 1 then r,g,b = c,x,0 114 | elseif h < 2 then r,g,b = x,c,0 115 | elseif h < 3 then r,g,b = 0,c,x 116 | elseif h < 4 then r,g,b = 0,x,c 117 | elseif h < 5 then r,g,b = x,0,c 118 | else r,g,b = c,0,x 119 | end return (r+m)*255,(g+m)*255,(b+m)*255 120 | end 121 | 122 | 123 | -- convert RGB to HSL 124 | local function RGBtoHSL(r, g, b) 125 | r, g, b = r/255, g/255, b/255 126 | local M, m = math.max(r, g, b), 127 | math.min(r, g, b) 128 | local c, H = M - m, 0 129 | if M == r then H = (g-b)/c%6 130 | elseif M == g then H = (b-r)/c+2 131 | elseif M == b then H = (r-g)/c+4 132 | end local L = 0.5*M+0.5*m 133 | local S = c == 0 and 0 or c/(1-math.abs(2*L-1)) 134 | return ((1/6)*H)*360%360, S*255, L*255 135 | end 136 | 137 | -- public interface 138 | luacolors.sRGBtoXYZ = sRGBtoXYZ 139 | luacolors.XYZtosRGB = XYZtosRGB 140 | luacolors.XYZtoLab = XYZtoLab 141 | luacolors.LabtoXYZ = LabtoXYZ 142 | luacolors.LabtosRGB = LabtosRGB 143 | luacolors.sRGBtoLab = sRGBtoLab 144 | luacolors.HSLtoRGB = HSLtoRGB 145 | luacolors.RGBtoHSL = RGBtoHSL 146 | 147 | 148 | -- colors 149 | -- taken from http://en.wikipedia.org/wiki/CSS_colors 150 | luacolors.white = {255, 255, 255} 151 | luacolors.silver = {191, 191, 191} 152 | luacolors.gray = {127, 127, 127} 153 | luacolors.black = {0, 0, 0} 154 | luacolors.red = {255, 0, 0} 155 | luacolors.maroon = {127, 0, 0} 156 | luacolors.yellow = {255, 255, 0} 157 | luacolors.olive = {127, 127, 0} 158 | luacolors.lime = {0, 255, 0} 159 | luacolors.green = {0, 127, 0} 160 | luacolors.aqua = {0, 255, 255} 161 | luacolors.teal = {0, 127, 127} 162 | luacolors.blue = {0, 0, 255} 163 | luacolors.navy = {0, 0, 127} 164 | luacolors.fuchsia = {255, 0, 255} 165 | luacolors.purple = {127, 0, 127} 166 | 167 | return luacolors --------------------------------------------------------------------------------