├── doc ├── colors.png └── howto.md ├── README.txt ├── rockspec ├── LICENSE.txt ├── petrodoc └── lua └── colors.lua /doc/colors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yuri/lua-colors/HEAD/doc/colors.png -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | This is a lua library for color theory calculations. 2 | Please see http://spu.tnik.org/lib/colors/ for more information. 3 | This module was originally a part of Sputnik project (http://spu.tnik.org) 4 | but is now being split into its own repository. 5 | 6 | -------------------------------------------------------------------------------- /rockspec: -------------------------------------------------------------------------------- 1 | package = "Colors" 2 | version = "8.05.26-0" 3 | source = { 4 | url = "http://sputnik.freewisdom.org/files/colors-8.05.26.tar.gz", 5 | } 6 | description = { 7 | summary = "HSL Color Theory Computation in Lua", 8 | detailed = [===[ Colors library provides methods to do color computation in 9 | HSL color space and for finding "harmonious" color palettes. ]===], 10 | license = "MIT/X11", 11 | homepage = "http://sputnik.freewisdom.org/lib/colors/", 12 | maintainer = "Yuri Takhteyev ", 13 | } 14 | dependencies = { 15 | } 16 | build = { 17 | type = "none", 18 | install = { 19 | lua = { 20 | 21 | ["colors"] = "lua/colors.lua", 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007, 2008 Yuri Takhteyev 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /petrodoc: -------------------------------------------------------------------------------- 1 | package = 'Colors' 2 | versions = { 3 | {'8.05.26', 'May 26, 2008', 'added ability to create colors from RGB codes'}, 4 | {'8.03.08', 'March 8, 2008', 'updated petrodoc, no functional changes'}, 5 | {'8.02.23', 'Feb. 23, 2008', 'added Luadoc comments'}, 6 | {'8.02.03', 'Feb. 2, 2008', 'cleaned up for a stand-alone release'} 7 | } 8 | summary = 'HSL Color Theory Computation in Lua' 9 | maintainer = 'Yuri Takhteyev ' 10 | 11 | detailed = [[ Colors library provides methods to do color computation in 12 | HSL color space and for finding "harmonious" color palettes. ]] 13 | license = 'MIT/X11' 14 | 15 | homepage = 'http://sputnik.freewisdom.org/lib/colors/' 16 | favicon = 'http://media.freewisdom.org/etc/sputnik-icon.png' 17 | download = 'http://sputnik.freewisdom.org/files/colors-$version.tar.gz' 18 | 19 | push = "scp %s yuri@web10.webfaction.com:~/webapps/static/files/" 20 | 21 | logo = 'http://sputnik.freewisdom.org/lib/colors/colors.png' 22 | keywords = 'Lua, HSL' 23 | rss = homepage.."releases.rss" 24 | -------------------------------------------------------------------------------- 25 | 26 | dependencies = "" 27 | 28 | Installation = [[ 29 | Colors consists of a single file (colors.lua) of slightly more 30 | than 100 lines, plus a test script (test.lua). 31 | Just put colors.lua somewhere in your Lua path. 32 | 33 | Here is a list of recent releases: 34 | 35 | 38 | 39 | You can also install it using LuaRocks with 40 | 41 | luarocks install colors 42 | 43 | or: 44 | 45 | luarocks --from=http://sputnik.freewisdom.org/rocks/earth install colors 46 | 47 | ]] ---------------------------------------------------------------------------- 48 | 49 | Contact = [[ 50 | For more information please contact the author, 51 | [Yuri Takhteyev](http://takhteyev.org/contact/) or write to the 52 | [Sputnik Mailing List](http://sputnik.freewisdom.org/en/Mailing_List). 53 | 54 | Comments are welcome! 55 | ]] ---------------------------------------------------------------------------- 56 | 57 | TOC = { 58 | { "Overview", "

"..detailed.."

" }, 59 | { "Installation", markdown(Installation) }, 60 | { "Using Colors", markdown(include("doc/howto.md")) }, 61 | { "Contact", markdown(Contact)}, 62 | { "LuaDoc", make_luadoc{"colors.lua"} }, 63 | { "License", markdown(include("LICENSE.txt")) } 64 | } 65 | ------------------------------------------------------------------------------- 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /lua/colors.lua: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------- 2 | -- Provides support for color manipulation in HSL color space. 3 | -- 4 | -- http://sputnik.freewisdom.org/lib/colors/ 5 | -- 6 | -- License: MIT/X 7 | -- 8 | -- (c) 2008 Yuri Takhteyev (yuri@freewisdom.org) * 9 | -- 10 | -- * rgb_to_hsl() implementation was contributed by Markus Fleck-Graffe. 11 | ----------------------------------------------------------------------------- 12 | 13 | local M = {} 14 | 15 | local Color = {} 16 | local Color_mt = {__metatable = {}, __index = Color} 17 | 18 | local rgb_string_to_hsl -- defined below 19 | 20 | ----------------------------------------------------------------------------- 21 | -- Instantiates a new "color". 22 | -- 23 | -- @param H hue (0-360) _or_ an RGB string ("#930219") 24 | -- @param S saturation (0.0-1.0) 25 | -- @param L lightness (0.0-1.0) 26 | -- @return an instance of Color 27 | ----------------------------------------------------------------------------- 28 | local function new(H, S, L) 29 | if type(H) == "string" and H:sub(1,1)=="#" and H:len() == 7 then 30 | H, S, L = rgb_string_to_hsl(H) 31 | end 32 | assert(Color_mt) 33 | return setmetatable({H = H, S = S, L = L}, Color_mt) 34 | end 35 | M.new = new 36 | 37 | ----------------------------------------------------------------------------- 38 | -- Converts an HSL triplet to RGB 39 | -- (see http://homepages.cwi.nl/~steven/css/hsl.html). 40 | -- 41 | -- @param H hue (0-360) 42 | -- @param S saturation (0.0-1.0) 43 | -- @param L lightness (0.0-1.0) 44 | -- @return an R, G, and B component of RGB 45 | ----------------------------------------------------------------------------- 46 | 47 | local function hsl_to_rgb(h, s, L) 48 | h = h/360 49 | local m1, m2 50 | if L<=0.5 then 51 | m2 = L*(s+1) 52 | else 53 | m2 = L+s-L*s 54 | end 55 | m1 = L*2-m2 56 | 57 | local function _h2rgb(m1, m2, h) 58 | if h<0 then h = h+1 end 59 | if h>1 then h = h-1 end 60 | if h*6<1 then 61 | return m1+(m2-m1)*h*6 62 | elseif h*2<1 then 63 | return m2 64 | elseif h*3<2 then 65 | return m1+(m2-m1)*(2/3-h)*6 66 | else 67 | return m1 68 | end 69 | end 70 | 71 | return _h2rgb(m1, m2, h+1/3), _h2rgb(m1, m2, h), _h2rgb(m1, m2, h-1/3) 72 | end 73 | M.hsl_to_rgb = hsl_to_rgb 74 | 75 | ----------------------------------------------------------------------------- 76 | -- Converts an RGB triplet to HSL. 77 | -- (see http://easyrgb.com) 78 | -- 79 | -- @param r red (0.0-1.0) 80 | -- @param g green (0.0-1.0) 81 | -- @param b blue (0.0-1.0) 82 | -- @return corresponding H, S and L components 83 | ----------------------------------------------------------------------------- 84 | 85 | local function rgb_to_hsl(r, g, b) 86 | --r, g, b = r/255, g/255, b/255 87 | local min = math.min(r, g, b) 88 | local max = math.max(r, g, b) 89 | local delta = max - min 90 | 91 | local h, s, l = 0, 0, ((min+max)/2) 92 | 93 | if l > 0 and l < 0.5 then s = delta/(max+min) end 94 | if l >= 0.5 and l < 1 then s = delta/(2-max-min) end 95 | 96 | if delta > 0 then 97 | if max == r and max ~= g then h = h + (g-b)/delta end 98 | if max == g and max ~= b then h = h + 2 + (b-r)/delta end 99 | if max == b and max ~= r then h = h + 4 + (r-g)/delta end 100 | h = h / 6; 101 | end 102 | 103 | if h < 0 then h = h + 1 end 104 | if h > 1 then h = h - 1 end 105 | 106 | return h * 360, s, l 107 | end 108 | M.rgb_to_hsl = rgb_to_hsl 109 | 110 | -- already local, see at the bottom 111 | function rgb_string_to_hsl(rgb) 112 | return rgb_to_hsl(tonumber(rgb:sub(2,3), 16)/255, 113 | tonumber(rgb:sub(4,5), 16)/255, 114 | tonumber(rgb:sub(6,7), 16)/255) 115 | end 116 | M.rgb_string_to_hsl = rgb_string_to_hsl 117 | 118 | ----------------------------------------------------------------------------- 119 | -- Converts the color to an RGB string. 120 | -- 121 | -- @return a 6-digit RGB representation of the color prefixed 122 | -- with "#" (suitable for inclusion in HTML) 123 | ----------------------------------------------------------------------------- 124 | 125 | function Color:to_rgb() 126 | local r, g, b = hsl_to_rgb(self.H, self.S, self.L) 127 | local rgb = {hsl_to_rgb(self.H, self.S, self.L)} 128 | local buffer = "#" 129 | for i,v in ipairs(rgb) do 130 | buffer = buffer..string.format("%02x",math.floor(v*255+0.5)) 131 | end 132 | return buffer 133 | end 134 | 135 | ----------------------------------------------------------------------------- 136 | -- Creates a new color with hue different by delta. 137 | -- 138 | -- @param delta a delta for hue. 139 | -- @return a new instance of Color. 140 | ----------------------------------------------------------------------------- 141 | function Color:hue_offset(delta) 142 | return new((self.H + delta) % 360, self.S, self.L) 143 | end 144 | 145 | ----------------------------------------------------------------------------- 146 | -- Creates a complementary color. 147 | -- 148 | -- @return a new instance of Color 149 | ----------------------------------------------------------------------------- 150 | function Color:complementary() 151 | return self:hue_offset(180) 152 | end 153 | 154 | ----------------------------------------------------------------------------- 155 | -- Creates two neighboring colors (by hue), offset by "angle". 156 | -- 157 | -- @param angle the difference in hue between this color and the 158 | -- neighbors 159 | -- @return two new instances of Color 160 | ----------------------------------------------------------------------------- 161 | function Color:neighbors(angle) 162 | local angle = angle or 30 163 | return self:hue_offset(angle), self:hue_offset(360-angle) 164 | end 165 | 166 | ----------------------------------------------------------------------------- 167 | -- Creates two new colors to make a triadic color scheme. 168 | -- 169 | -- @return two new instances of Color 170 | ----------------------------------------------------------------------------- 171 | function Color:triadic() 172 | return self:neighbors(120) 173 | end 174 | 175 | ----------------------------------------------------------------------------- 176 | -- Creates two new colors, offset by angle from this colors complementary. 177 | -- 178 | -- @param angle the difference in hue between the complementary and 179 | -- the returned colors 180 | -- @return two new instances of Color 181 | ----------------------------------------------------------------------------- 182 | function Color:split_complementary(angle) 183 | return self:neighbors(180-(angle or 30)) 184 | end 185 | 186 | ----------------------------------------------------------------------------- 187 | -- Creates a new color with saturation set to a new value. 188 | -- 189 | -- @param saturation the new saturation value (0.0 - 1.0) 190 | -- @return a new instance of Color 191 | ----------------------------------------------------------------------------- 192 | function Color:desaturate_to(saturation) 193 | return new(self.H, saturation, self.L) 194 | end 195 | 196 | ----------------------------------------------------------------------------- 197 | -- Creates a new color with saturation set to a old saturation times r. 198 | -- 199 | -- @param r the multiplier for the new saturation 200 | -- @return a new instance of Color 201 | ----------------------------------------------------------------------------- 202 | function Color:desaturate_by(r) 203 | return new(self.H, self.S*r, self.L) 204 | end 205 | 206 | ----------------------------------------------------------------------------- 207 | -- Creates a new color with lightness set to a new value. 208 | -- 209 | -- @param lightness the new lightness value (0.0 - 1.0) 210 | -- @return a new instance of Color 211 | ----------------------------------------------------------------------------- 212 | function Color:lighten_to(lightness) 213 | return new(self.H, self.S, lightness) 214 | end 215 | 216 | ----------------------------------------------------------------------------- 217 | -- Creates a new color with lightness set to a old lightness times r. 218 | -- 219 | -- @param r the multiplier for the new lightness 220 | -- @return a new instance of Color 221 | ----------------------------------------------------------------------------- 222 | function Color:lighten_by(r) 223 | return new(self.H, self.S, self.L*r) 224 | end 225 | 226 | ----------------------------------------------------------------------------- 227 | -- Creates n variations of this color using supplied function and returns 228 | -- them as a table. 229 | -- 230 | -- @param f the function to create variations 231 | -- @param n the number of variations 232 | -- @return a table with n values containing the new colors 233 | ----------------------------------------------------------------------------- 234 | function Color:variations(f, n) 235 | n = n or 5 236 | local results = {} 237 | for i=1,n do 238 | table.insert(results, f(self, i, n)) 239 | end 240 | return results 241 | end 242 | 243 | ----------------------------------------------------------------------------- 244 | -- Creates n tints of this color and returns them as a table 245 | -- 246 | -- @param n the number of tints 247 | -- @return a table with n values containing the new colors 248 | ----------------------------------------------------------------------------- 249 | function Color:tints(n) 250 | local f = function (color, i, n) 251 | return color:lighten_to(color.L + (1-color.L)/n*i) 252 | end 253 | return self:variations(f, n) 254 | end 255 | 256 | ----------------------------------------------------------------------------- 257 | -- Creates n shades of this color and returns them as a table 258 | -- 259 | -- @param n the number of shades 260 | -- @return a table with n values containing the new colors 261 | ----------------------------------------------------------------------------- 262 | function Color:shades(n) 263 | local f = function (color, i, n) 264 | return color:lighten_to(color.L - (color.L)/n*i) 265 | end 266 | return self:variations(f, n) 267 | end 268 | 269 | function Color:tint(r) 270 | return self:lighten_to(self.L + (1-self.L)*r) 271 | end 272 | 273 | function Color:shade(r) 274 | return self:lighten_to(self.L - self.L*r) 275 | end 276 | 277 | Color_mt.__tostring = Color.to_rgb 278 | 279 | -- allow to use `colors(...)` instead of `colors.new(...)` 280 | setmetatable(M, {__call=function(_, ...) return new(...) end}) 281 | 282 | return M 283 | -------------------------------------------------------------------------------- /doc/howto.md: -------------------------------------------------------------------------------- 1 | Colors are typically encoded in software by their coordinates in the RGB (Red-Green-Blue) color space. Unfortunately, the RGB color space is very 2 | unintuitive and additionally certain principles of color harmony do not express themselves easily in RGB. The HSL (Hue-Saturation-Lightness) color 3 | space solves both of those problems. This library allows you to work with colors in the HSL space, calculate harmonious patterns and then convert 4 | the result to RGB. (See [worqx.com](http://www.worqx.com/color/color_wheel.htm) for some background on color harmony.) 5 | 6 | ## The HSL Color Space 7 | 8 | Colors are encoded in HSL by three values: Hue, Saturation and Lightness. 9 | 10 |
11 | 12 | **Lightness** is just the opposite of darkness of the color. White has lightness 1, black has lightness 0. Other colors are inbetween: 13 | **Saturation** is the intensity of color, which shows how far the color is from gray. Here are the shades of red for different saturation and lightness: 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 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 |
Lightness
  0.00.10.20.30.40.50.60.70.80.91.0
Saturation0.0           
0.1           
0.2           
0.3           
0.4           
0.5           
0.6           
0.7           
0.8           
0.9           
1.0           
199 | 200 | **Hue** is the "color" of color: what makes "green" different from "red". Hue can also be expressed as a number between 0 and 1, though this library uses the values from 0 to 360 instead. 201 | Unlike lightness and saturdation, hue _loops_: the hue of 360 is actually the same color as hue of 0 (red). 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 |
Saturation
  1.00.90.80.70.60.50.40.30.20.10.0
Hue0           
10           
20           
30           
40           
50           
60           
70           
80           
90           
100           
110           
120           
130           
140           
150           
160           
170           
180           
190           
200           
210           
220           
230           
240           
250           
260           
270           
280           
290           
300           
310           
320           
330           
340           
350           
360           
777 | 778 | ## HSL Calculations with Colors 779 | 780 | Creating a color in HSL space and converting it to RGB 781 | 782 | > local colors = require("colors") 783 | > c = colors.new(130, .8, 0.3) -- green, pretty saturated, somewhat dark 784 | > =tostring(c) 785 | #0f8923 786 | 787 | 788 | 789 |
 
790 | 791 | You can also create this color from it's RGB code: 792 | 793 | > local colors = require("colors") 794 | > c = colors.new("#0f8923") -- green, pretty saturated, somewhat dark 795 | > =tostring(c) 796 | #0f8923 797 | 798 | The color converts to its RGB representation when forced into a string: 799 | 800 | > =c -- convert implicitly 801 | #0f8923 802 | 803 | 804 |
 
805 | 806 | Accessing the HSL components: 807 | 808 | > print(c.H, c.S, c.L) 809 | 130 0.8 0.3 810 | 811 | Changing saturation: 812 | 813 | > =c:desaturate_by(.5) -- set saturation to saturation*.5 814 | #2d6b38 815 | 816 | 817 |
 
818 | 819 | > =c:desaturate_to(.5) -- set saturation to .5 820 | #267233 821 | 822 | 823 |
 
824 | 825 | Changing lightness: 826 | 827 | > =c:lighten_by(.5) -- set lightness to lightness*.5 828 | #14b72f 829 | 830 | 831 |
 
832 | 833 | > =c:lighten_to(.5) -- set lightness to .5 834 | #19e53b 835 | 836 | 837 |
 
838 | 839 | Changing hue: 840 | 841 | > =c:hue_offset(180) -- shift hue by 180 842 | #890f75 843 | 844 | 845 |
 
846 | 847 | ## Building Color Schemes 848 | 849 | To build a color scheme, we usually start with a color, pick one or more matching colors, then derive shades and tints from them. You might want to read up on color combinations at [worqx.com](http://www.worqx.com/color/combinations.htm 850 | ). 851 | 852 | For a **monochromatic** color scheme, we'll just use the color we started with and tints and shades from it: 853 | 854 | > tints = c:tints(5) -- make five tints 855 | > for i,t in ipairs(tints) do print(t) end 856 | #16c934 857 | #3ee95a 858 | #7ef091 859 | #bef7c8 860 | #ffffff 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 |
     
872 | 873 | > shades = c:shades(5) -- make five shades 874 | > for i,s in ipairs(shades) do print(s) end 875 | #0c6e1c 876 | #095215 877 | #06370e 878 | #031b07 879 | #000000 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 |
     
891 | 892 | For a **complimentary** scheme, we can use easily derive a complimentary color and its tints and shades: 893 | 894 | > ctints = c:complementary():tints(5) -- make five tints of the complimentary color 895 | > for i,t in ipairs(ctints) do print(t) end 896 | #c916ac 897 | #e93ecd 898 | #f07edd 899 | #f7beee 900 | #ffffff 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 |
     
     
919 | 920 | For less contrast, though, we might want to stick with **neighboring** colors: e.g., +/- 60 degrees of the starting color: 921 | 922 | > n1, n2 = c:neighbors(60) -- get neiboring colors: 60 degees up and down 923 | > for i,t in ipairs(n1:tints()) do print(t) end 924 | #16c98e 925 | #3ee9b0 926 | #7ef0ca 927 | #bef7e4 928 | #ffffff 929 | > for i,t in ipairs(n2:tints()) do print(t) end 930 | #52c916 931 | #77e93e 932 | #a4f07e 933 | #d1f7be 934 | #ffffff 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 |
     
     
     
961 | 962 | We could alternatively generate a split complementary color scheme: 963 | 964 | > for i,t in ipairs(c1:tints()) do print(t) end 965 | #8e16c9 966 | #b03ee9 967 | #ca7ef0 968 | #e4bef7 969 | #ffffff 970 | > for i,t in ipairs(c2:tints()) do print(t) end 971 | #c91652 972 | #e93e77 973 | #f07ea4 974 | #f7bed1 975 | #ffffff 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 |
     
     
     
1002 | 1003 | 1004 | Or a triadic one: 1005 | 1006 | > t1, t2 = c:triadic() 1007 | > for i,t in ipairs(t1:tints()) do print(t) end 1008 | #3416c9 1009 | #5a3ee9 1010 | #917ef0 1011 | #c8bef7 1012 | #ffffff 1013 | > for i,t in ipairs(t2:tints()) do print(t) end 1014 | #c93416 1015 | #e95a3e 1016 | #f0917e 1017 | #f7c8be 1018 | #ffffff 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 |
     
     
     
1045 | 1046 | --------------------------------------------------------------------------------