Page 1 of 1

Negative color

Posted: Fri Dec 04, 2015 10:17 am
by Fab1can
How can i color a text (created with love.graphics.print) with the negative of the backgruond?

Re: Negative color

Posted: Fri Dec 04, 2015 10:52 am
by Zeliarden
Like this

Code: Select all

r, g, b, a = love.graphics.getBackgroundColor( )
love.graphics.setColor( 255-r, 255-g, 255-b)

Re: Negative color

Posted: Fri Dec 04, 2015 11:05 am
by Fab1can
Zeliarden wrote:Like this

Code: Select all

r, g, b, a = love.graphics.getBackgroundColor( )
love.graphics.setColor( 255-r, 255-g, 255-b)
I wasn't mean this, for background i mean everything that is behind the number (it isn't just one color)

Re: Negative color

Posted: Fri Dec 04, 2015 5:26 pm
by rok
You could maybe achieve something like this using two images for example. One for the background and one for the text or whatever.

ImageData has https://love2d.org/wiki/ImageData:setPixel and https://love2d.org/wiki/ImageData:getPixel

If you keep the starting position of both images (top left corner for example) you could read/write colors for the pixels you want to change (overlapping).

Just read the color on the bottom image and set the color on the top one to the inverse.

This wouldn't be good for performance I guess but it should work.

Re: Negative color

Posted: Fri Dec 04, 2015 9:51 pm
by pgimeno
Maybe something like this helps?

Code: Select all

  love.graphics.setBlendMode('subtractive')
Edit: Hm, maybe not.

A shader and a canvas is probably the way to go. Stay tuned.

Edit2: Here we go:

Code: Select all

local img, canvas, shader
local lg = love.graphics

function love.load()
  canvas = lg.newCanvas()
  img = lg.newImage("image1.jpg")
  lg.setFont(lg.newFont(80))
  shader = lg.newShader
[[
  extern Image bgimg;

  vec4 effect(vec4 clr, Image img, vec2 imgpos, vec2 scrpos)
  {
    float alpha = Texel(img, imgpos).w * clr.w;
    vec4 bgpixel = Texel(bgimg, vec2(scrpos.x / love_ScreenSize.x, 1. - scrpos.y/love_ScreenSize.y));
    return vec4(vec3(1., 1., 1.) - bgpixel.xyz, alpha);
  }
]]
end


function love.draw()
  -- This represents drawing the background to the canvas
  lg.setCanvas(canvas)
  lg.draw(img)
  lg.setCanvas()

  -- Send the background to the shader
  shader:send('bgimg', canvas)

  -- Draw the background on the actual screen
  lg.draw(canvas)

  -- Apply the shader to the text
  lg.setShader(shader)
  lg.print("█*#%█", 450, 0)
  lg.setShader()
end