Advertisement
Anatoly03

Colors v3

Oct 29th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.43 KB | None | 0 0
  1.  
  2. --# README
  3. --[[
  4.  
  5. Author:      Anatoly
  6. License:     ISC
  7. Description: Color()s is a function that helps you accessing all the colors very easily.
  8. Thread:      https://codea.io/talk/discussion/10043/colors
  9. Release:     3
  10.  
  11. Changes:
  12. * warm-white
  13. * cold-white
  14. * white-10 to white-90 now support prerixes. e.g. reddish-white-10 to reddish-white-90 are shades of red
  15. * first version of the search box out! open in the output panel to searc for colors. Not working yet, but some values can be already searched through!
  16. * Fast access functions Fill(), Stroke(), Tint()
  17. * New color instances: "seablue", "bronze", "silver", "gold", "diamondblue", "gray" and "grey" (= white-50)
  18.  
  19. ]]
  20. --# Colors
  21. _colors = {}
  22.  
  23. -- <COLOR>
  24.  
  25. colors1 = {}
  26.  
  27. -- Primary & Secondary Colors
  28. colors1["red"] = color(255, 0, 0, 255)
  29. colors1["yellow"] = color(255, 255, 0, 255)
  30. colors1["green"] = color(0, 255, 0, 255)
  31. colors1["cyan"] = color(0, 255, 255, 255)
  32. colors1["blue"] = color(0, 0, 255, 255)
  33. colors1["magenta"] = color(255, 0, 255, 255)
  34.  
  35. -- White scale
  36. colors1["black"] = color(0, 255)
  37. colors1["white"] = color(255, 255)
  38.  
  39. -- Generate Grey Scale
  40. for i = 1, 9 do
  41.     colors1["white-" .. i .. "0"] = color(25.5 * i, 255)
  42.     --table.insert(_colors, {n="white-" .. i .. "0",c=colors1["white-" .. i .. "0"]})
  43. end
  44.  
  45. colors1["gray"] = colors1["white-50"] -- Support BE and AE
  46. colors1["grey"] = colors1["white-50"]
  47.  
  48. -- White shades
  49. colors1["warm-white"] = color(219, 211, 187, 255)
  50. colors1["cold-white"] = color(192, 212, 225, 255)
  51.  
  52. -- Tertiary Colors
  53.  
  54. --colors1["middlegreen"] = color(150, 155, 0, 255)
  55.  
  56. colors1["grassgreen"] = color(150, 255, 0, 255)
  57. colors1["forestgreen"] = color(0, 155, 0, 255)
  58.  
  59. colors1["turquoise"] = color(0, 255, 150, 255)
  60. colors1["skyblue"] = color(0, 150, 255, 255)
  61. colors1["seablue"] = color(50, 200, 255)
  62.  
  63. colors1["orange"] = color(255, 150, 0, 255)
  64. colors1["rosa"] = color(255, 0, 150, 255)
  65. colors1["purple"] = color(150, 0, 255, 255)
  66. colors1["violet"] = colors1["purple"]
  67. colors1["pink"] = color(255, 150, 255, 255)
  68.  
  69. colors1["bronze"] = color(190, 155, 90, 255)
  70. colors1["silver"] = color(180, 210, 210, 255)
  71. colors1["gold"] = color(225, 180, 60, 255)
  72. colors1["diamondblue"] = color(95, 170, 225, 255)
  73.  
  74. -- Quartiary Colors
  75.  
  76. --[[
  77. colors1["orange"] = color(255, 150, 255, 255)
  78. colors1["orange"] = color(150, 255, 150, 255)
  79. colors1["orange"] = color(150, 150, 255, 255)
  80.   ]]
  81.  
  82. colors2 = {}
  83.  
  84. -- Generate Dark & Light Versions.
  85. for a, b in pairs(colors1) do
  86.     colors2[a] = b
  87.     colors2["dark-" .. a] = b:mix(color(0, 255), 0.5)
  88.     colors2["light-" .. a] = b:mix(color(255, 255), 0.5)
  89.     colors2["middle-" .. a] = b:mix(color(127, 255), 0.5)
  90.     colors2["invert-" .. a] = color(255-b.r, 255-b.g, 255-b.b, 255)
  91.    
  92.     colors2["reddish-" .. a] = b:mix(color(255, 0, 0, 255), 0.6)
  93.     colors2["greenish-" .. a] = b:mix(color(0, 255, 0, 255), 0.6)
  94.     colors2["blueish-" .. a] = b:mix(color(0, 0, 255, 255), 0.6)
  95.    
  96.     table.insert(_colors, {n=a,c=b})
  97.    
  98.     for c, d in ipairs({"dark", "middle", "light", "reddish", "greenish", "blueish"}) do
  99.         table.insert(_colors, {n=d.."-"..a,c=colors2[d.."-"..a]})
  100.     end
  101. end
  102.  
  103. colors3 = colors2
  104.  
  105. -- <EARLIER>Invert all possible colors</EARLIER>
  106. --[[for a, b in pairs(colors2) do
  107.     colors3[a] = b
  108.     --colors3["invert-" .. a] = color(255-b.r, 255-b.g, 255-b.b, 255)
  109.    
  110.     --table.insert(_colors, {n="invert-"..a,c=colors3["invert-"..a]})
  111. end]]
  112.  
  113. -- Export
  114. colors = colors3
  115.  
  116. colors1 = nil
  117. colors2 = nil
  118. colors3 = nil
  119.  
  120.    
  121. -- Sort alphabetically.
  122. table.sort(_colors, function(a, b)
  123.     return a.n < b.n
  124. end)
  125.  
  126. -- </COLOR>
  127.  
  128. -- Initialise
  129.  
  130. Hex = function(l)
  131.     if not (type(l) == "string") then
  132.         assert(false, "bad argument #1 to 'Hex' (string expected, got ".. type(l) ..")")
  133.     end
  134.    
  135.     local col = tonumber(l, 16)
  136.    
  137.     local b = col%256
  138.     local g = ((col - b)/256)%256
  139.     local r = ((col - g - b)/256^2)
  140.    
  141.     return color(r, g, b, 255)
  142. end
  143.  
  144. Color = function(s, alpha)
  145.     local c = color(0, 255)
  146.    
  147.     if not (type(s) == "string") then
  148.         assert(false, "bad argument #1 to 'Color' (string expected, got ".. type(s) ..")")
  149.     end
  150.     s = string.lower(s)
  151.     if colors[s] then c = colors[s] end
  152.     if string.sub(s, 1, 1) == "#" and #s > 1 then c = Hex(string.sub(s, 2, #s)) end
  153.    
  154.     if alpha then c.a = alpha end
  155.    
  156.     return c
  157. end
  158.  
  159. -- Fast access implementations
  160.  
  161. function Fill(s, a) fill(Color(s, a)) end
  162. function Stroke(s, a) stroke(Color(s, a)) end
  163. function Tint(s, a) tint(Color(s, a)) end
  164.  
  165. --# Main
  166. -- colors1
  167.  
  168. function setup()
  169.     displayMode(FULLSCREEN)
  170.    
  171.     parameter.text("SEARCH", "", search)
  172.     --parameter.watch("1/DeltaTime")
  173.    
  174.     scrollY = 0
  175.     scrollYAcc = 0
  176.     scrollYAccMax = 100
  177.     scrollYAccMin = -100
  178.    
  179.     touching = false
  180.     infoState = 2
  181.     infoVisibility = 0
  182.    
  183.     COLORS = _colors
  184.    
  185.     local __different = {}
  186.     for a, b in pairs(colors) do
  187.         local unique = true
  188.         for c, d in ipairs(__different) do
  189.             if d.r == b.r and d.g == b.g and d.b == b.b then
  190.                 unique = false
  191.             end
  192.         end
  193.        
  194.         if unique then
  195.             table.insert(__different, b)
  196.         end
  197.     end
  198.    
  199.     print("There are " .. #__different .. " unique colors.")
  200. end
  201.  
  202. function search(h)
  203.     print(#_colors)
  204.     c = {}
  205.     for a, b in ipairs(_colors) do c[a]=b end
  206.    
  207.     if #h > 0 then
  208.         r = 0
  209.         for v = 1, #c do
  210.             if v <= #c and (not string.find(c[v - r].n, h:gsub("%-", "%%-"))) then
  211.                 table.remove(c, v - r)
  212.                 r = r + 1
  213.             elseif v > #c then
  214.                 break
  215.             end
  216.         end
  217.     end
  218.    
  219.     COLORS = c
  220.     print("SEARCH FOR '" .. h .. "' GAVE " .. #COLORS .. " RESULTS")
  221. end
  222.  
  223. function showInfo()
  224.     infoState = 1
  225. end
  226.  
  227. function hideInfo()
  228.     infoState = 2
  229. end
  230.  
  231. function draw()
  232.     displayRows = math.floor(HEIGHT/50)+3
  233.    
  234.     scrollY = scrollY + scrollYAcc
  235.     if scrollYAcc < 1.5 and not touching then
  236.         scrollYAcc = scrollYAcc + .05
  237.     elseif touching then
  238.         scrollYAcc = scrollYAcc * .8
  239.     end
  240.    
  241.     if scrollYAcc > scrollYAccMax then
  242.         scrollYAcc = scrollYAccMax
  243.     elseif scrollYAcc < scrollYAccMin then
  244.         scrollYAcc = scrollYAccMin
  245.     end
  246.    
  247.     if scrollY > #COLORS * 50 + 100 then
  248.         scrollY = -HEIGHT
  249.         scrollYAcc = 2
  250.     elseif scrollY < -HEIGHT then
  251.         scrollY = -HEIGHT
  252.         scrollYAcc = 0
  253.         showInfo()
  254.     end
  255.    
  256.     if scrollY > 100-HEIGHT then
  257.         hideInfo()
  258.     end
  259.    
  260.     if infoState == 1 and infoVisibility < 20 then
  261.         infoVisibility = infoVisibility + 1
  262.     elseif infoState == 2 and infoVisibility > 0 then
  263.         infoVisibility = infoVisibility - 1
  264.     end
  265.    
  266.     background(0)
  267.     for a, b in ipairs(COLORS) do
  268.         -- IGNORE IF OUTSIDE OF RANGE
  269.         if (scrollY - 50 * a < 50 and HEIGHT - 50 * a + scrollY > -50) and b.c then
  270.             -- BG
  271.             strokeWidth(40)
  272.             stroke(b.c or color(0, 255))
  273.             line(50, HEIGHT - 50 * a + scrollY, WIDTH-50, HEIGHT - 50 * a + scrollY)
  274.            
  275.             -- TEXT
  276.             strokeWidth(0)
  277.             --<EARLIER>fill(colors["invert-"..b.n] or color(0, 0))</EARLIER>
  278.             fill(0)
  279.             if (b.c.r <= 50 and b.c.g <= 50 and b.c.b <= 50) then
  280.                 fill(255)
  281.             end
  282.             fontSize(40)
  283.             font("CourierNewPS-BoldMT")
  284.             text(b.n, WIDTH*.5, HEIGHT - 50 * a + scrollY)
  285.            
  286.             -- COMPARISERS
  287.             for d, e in ipairs({"white", "white-50", "black", "red", "green", "blue"}) do
  288.                 fill(Color(e))
  289.                 ellipse(50 + (d-1) * 25, HEIGHT - 50 * a + scrollY, 20)
  290.             end
  291.         end
  292.     end
  293.    
  294.     if infoVisibility > 0 then
  295.         fill(Color("blueish-red", 255 * (infoVisibility / 20)))
  296.         fontSize(100)
  297.         font("MarkerFelt-Wide")
  298.         text("Colors v3", WIDTH*.5, HEIGHT*.5)
  299.        
  300.         fill(Color("blueish-forestgreen", 255 * (infoVisibility / 20)))
  301.         fontSize(50)
  302.         font("Noteworthy-Light")
  303.         text("Created by Anatoly.", WIDTH*.5, HEIGHT*.5-100)
  304.     end
  305. end
  306.  
  307. function touched(t)
  308.     if t.state == BEGAN then
  309.         touching = true
  310.     elseif t.state == MOVING then
  311.         scrollYAcc = scrollYAcc + t.deltaY *.2
  312.     else -- END
  313.         touching = false
  314.     end
  315. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement