Advertisement
Light83

Colorutils Lua (OpenComputer LanteaCraft)

Apr 19th, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. --[[
  2. colorutils
  3.  
  4. a simple library with some useful functions for dealing with colors.
  5. Shared by gfxbuffer and canvas, and possibly gml later?
  6.  
  7. --]]
  8. local colorutils = {VERSION="1.0"}
  9.  
  10. function colorutils.convColor_hto8(hex)
  11. local r,g,b=bit32.rshift(hex,16),bit32.rshift(hex,8)%256,hex%256
  12. r=round(r*7/255)
  13. g=round(g*7/255)
  14. b=round(b*3/255)
  15. return r*32+g*4+b
  16. end
  17.  
  18. function colorutils.convColor_8toh(c)
  19. local r,g,b=math.floor(c/32),math.floor(c/4)%8,c%4
  20. r=round(r*255/7)
  21. g=round(g*255/7)
  22. b=round(b*255/3)
  23. return r*65536+g*256+b
  24. end
  25.  
  26. function colorutils.convColor_hto4(hex)
  27. local r,g,b=bit32.rshift(hex,16),bit32.rshift(hex,8)%256,hex%256
  28. r=round(r/255)
  29. g=round(g*3/255)
  30. b=round(b/255)
  31. return r*8+g*2+b
  32. end
  33.  
  34. function colorutils.convColor_4toh(c)
  35. local r,g,b=math.floor(c/8),math.floor(c/2)%4,c%2
  36. r=r*0xff0000
  37. g=round(g*0xff/3)*256
  38. b=b*0x0000ff
  39. return r+g+b
  40. end
  41.  
  42. function colorutils.convColor_hto1(hex)
  43. return hex<0 and 1 or 0
  44. end
  45.  
  46. function colorutils.convColor_1toh(c)
  47. return c<0 and 0xffffff or 0
  48. end
  49.  
  50. return colorutils
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement