Negative color

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Fab1can
Prole
Posts: 7
Joined: Sat Nov 07, 2015 5:58 pm
Location: Italy

Negative color

Post by Fab1can »

How can i color a text (created with love.graphics.print) with the negative of the backgruond?
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Negative color

Post by Zeliarden »

Like this

Code: Select all

r, g, b, a = love.graphics.getBackgroundColor( )
love.graphics.setColor( 255-r, 255-g, 255-b)
Fab1can
Prole
Posts: 7
Joined: Sat Nov 07, 2015 5:58 pm
Location: Italy

Re: Negative color

Post 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)
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Negative color

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Negative color

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 84 guests