Spectra - Color Manager for LOVE2D

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Ostego160
Prole
Posts: 14
Joined: Tue Oct 17, 2017 7:18 pm

Spectra - Color Manager for LOVE2D

Post by Ostego160 » Fri Jan 05, 2018 6:15 pm

Spectra Color Manager

Greetings! I made a github repo for a color tool I made and use in my projects to be primarily used with the setColor() function. I originally made it to create simple tiled lighting effects but I also use it to store colors by name (e.g. All enemies should be 'red, while allies are 'blue'). The best features though are the color mixing tools; there are two: mix() and gradient(). The first mixes two colors while the latter will interpolate between 2-4 colors based on a distance/range. There are also RGB clamp settings that are applied with mix() and gradient().

Here is some sample code:

Code: Select all

Spectra = require('spectra')
spectra = Spectra()
--Init with default palette

spectra:edit('enemy', spectra:get('red'))

love.graphics.setColor( spectra:get('enemy', 128) )
--The color has been set to enemy(red) with half opacity

love.graphics.setColor ( spectra:mix('purple', 255, 300, 'green', .5) )
--The color has been set to a purple/green mix with increased brightness

love.graphics.setColor( spectra:gradient(512, 256, {'aqua','blue','midnight','black'}))
--The color has been set to midway between 'blue' and 'midnight' in the gradient and
--will interpolate between all four colors based on distance

spectra:clamp('darkgray', .5,  'lightblue', .25)
--A 50% darkgray minimum color has been applied
--A 25% lightblue maximum color has been applied
There are also additional demonstrations in my signature.

Please forgive if this is confusing; a better explanation exists on the github repo. I have also attached a functional demo. Also, please feel free to give me feedback; I'm still learning! :awesome:

Spectra Color Manager Github Repo
Attachments
Spectra Demo.love
(3.82 KiB) Downloaded 66 times

User avatar
Bunnybacon
Prole
Posts: 16
Joined: Fri Mar 25, 2016 8:42 am

Re: Spectra - Color Manager for LOVE2D

Post by Bunnybacon » Fri Jan 05, 2018 9:53 pm

Wow, that's really cool! can't wait to try it out in my own project! Thanks!

User avatar
yetneverdone
Party member
Posts: 320
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Spectra - Color Manager for LOVE2D

Post by yetneverdone » Sun Jan 07, 2018 11:10 am

Very useful and cool lib!

I have a question, how do you actually use the gradient function? How do you draw it exactly? Like use it as the background color?

Ostego160
Prole
Posts: 14
Joined: Tue Oct 17, 2017 7:18 pm

Re: Spectra - Color Manager for LOVE2D

Post by Ostego160 » Sun Jan 07, 2018 5:54 pm

yetneverdone wrote:
Sun Jan 07, 2018 11:10 am
Very useful and cool lib!

I have a question, how do you actually use the gradient function? How do you draw it exactly? Like use it as the background color?
Thanks! The gradient is used for more individual object drawing like tiles or world objects. I iterate through each object/tile and apply a setColor(). The gradient is observable when there are multiple elements being colored.

In an effort to follow Kikito's Rules and keep Spectra more or less simple, I migrated some of Spectra's features (including screen filtering / pixel mapping) to another module that I will be releasing shortly.

In the meantime, here is an idea on how to do it until I get off my lazy behind and fix/release the sister module:

Code: Select all

Spectra =   require('spectra')

local lg = love.graphics
local push = table.insert
-------------------------------
local function backgroundGradient(colorTable)
   local vertex = {}                --Table to hold texture data
   local w,h = lg.getDimensions()   --Size of the target surface

   push(vertex, {0,0,0,0,  spectra:get(colorTable[1]) }) --Top Left Corner
   push(vertex, {w,0,w,0,  spectra:get(colorTable[2]) }) --Top Right Corner
   push(vertex, {w,h,w,h,  spectra:get(colorTable[3]) }) --Bottom Right Corner
   push(vertex, {0,h,0,h,  spectra:get(colorTable[4]) }) --Bottom Left Corner

   local texture = lg.newMesh(vertex, 'fan') --Create new mesh with color data
   return texture                            --Return the mesh
end

function love.load()
   spectra = Spectra()                       -- Initialize Spectra
   background = backgroundGradient({'green', 'purple', 'lightblue', 'yellow'})
   --Call the background function with the named color values from Spectra
end

function love.draw()
   lg.draw(background)  --Draw Background (first)
end
Hope this is helpful!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests