Some help with my color is not changing

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
jsouth1722
Prole
Posts: 2
Joined: Thu Jan 27, 2022 12:23 am

Some help with my color is not changing

Post by jsouth1722 »

Hello everyone! I am completely new to lua and love2d, but I am trying to learn--going off some courses that I have purchased and reading. While tinkering with the language, I made a very simple program that bounces a circle off of the walls and then changes its color...or at least is supposed to change color. The weird thing is, it works and changes to a random color if I run this program in repl it, but it does not change to anything other than white if I run it in the actual love2d.exe. Could someone help me understand what is going on that is causing it to not change colors? I can even see that the variables I am using for the rgb is successfully changing. It's just not drawing the circle that way.

In summary: circle moves successfully and changes color successfully in repl.it. Circle moves successfully but does not change colors in love2d.exe.

Much appreciated ahead of time!

Code: Select all

success = love.window.setMode(1280, 720)
math.randomseed(os.time())

minWidth = 50
maxWidth = 1230
minHeight = 50
maxHeight = 670

x = math.random(minWidth, maxWidth)  --used for x coord
y = math.random(minHeight, maxHeight)  --used for y coord

a = 1
b = 1

r = 255 --red color var
g = 0   --green color var
bc = 0  --blue color var

function love.update()  
  x = x + a
  y = y + b 

  if x >= maxWidth then
    r = math.random(0, 255) --meant to set red to random value once ball bounces
    g = math.random(0, 255)  --meant to set green to random value once ball bounces
    bc = math.random(0, 255)  --meant to set blue to random value once ball bounces
  
    if a < 0 then
      a = a + -0.5  

    elseif a > 0 then
      a = a + 0.5
    end 

    a = a * -1 

  elseif x <= minWidth then

    r = math.random(0, 255) 
    g = math.random(0, 255)
    bc = math.random(0, 255)
    
    if a < 0 and a > -10 then
      a = a + -0.5

    elseif a > 0 and a < 10 then
      a = a + 0.5
    end

    a = a * -1
  end

 

  if y >= maxHeight then

    r = math.random(0, 255)
    g = math.random(0, 255)
    bc = math.random(0, 255)
   
    if b < 0 and b > -10 then
      b = b + -0.5

    elseif b > 0 and b < 10 then
      b = b + 0.5

    end 

    b = b * -1

  elseif y <= minHeight then

    r = math.random(0, 255)
    g = math.random(0, 255)
    bc = math.random(0, 255)
    
    if b < 0 and b > -10 then
      b = b + -0.5

    elseif b > 0 and b < 10 then
      b = b + 0.5

    end

    b = b * -1

  end
  
end

 

function love.draw()      
      love.graphics.print("r "..r.."g "..g.."b "..bc) --printing values of rgb to test the random generation
      love.graphics.setColor(r, g, bc) --trying to assign random colors to ball upon bouncing off of boundaries
      love.graphics.circle("fill", x, y, 50, 100)
      love.graphics.setColor(r, g, bc)

end
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Some help with my color is not changing

Post by MrFariator »

A fair while back, LÖVE switched from using the 0-255 color range to 0-1 range, and it's noted on the wiki on the love.graphics.setColor page.

So, to fix your code, you'll either have to generate numbers between 0 and 1 (like with love.math.random), or in your drawing function do the following:

Code: Select all

love.graphics.setColor(r/255, g/255, bc/255)
Of course, there's also the option to override the behavior setColor with the following:

Code: Select all

local originalFunction = love.graphics.setColor
love.graphics.setColor = function ( r, g, b, a )
  -- making sure none of these are nil
  r = r or 255
  g = g or 255
  b = b or 255
  a = a or 255
  -- call the original function and values by 255
  originalFunction ( r / 255, g / 255, b / 255, a / 255 )
end
If you put that somewhere in your code before a call to love.graphics.setColor is made, it'll let you use 0-255 color range instead. It'll be a bit slower than without this modification, but not all that significantly so.
jsouth1722
Prole
Posts: 2
Joined: Thu Jan 27, 2022 12:23 am

Re: Some help with my color is not changing

Post by jsouth1722 »

Thank you so much! Sorry, I tried doing some reading before posting but I should have done more I guess. Thank you for your help and patience!
User avatar
dusoft
Party member
Posts: 482
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Some help with my color is not changing

Post by dusoft »

Even better:

use

Code: Select all

love.math.colorFromBytes(0, 0, 0) -- => black

love.graphics.setColor(love.math.colorFromBytes(0, 0, 0))

love.graphics.setColor(love.math.colorFromBytes(255, 255, 255, 0.5)) -- white, rgb, alpha transparency 50%
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests