Alpha not respected in previously working utility function (0.78)

This forum is for the Lua scriptable clone of DM/CSB called Dungeon Strikes Back by Sophia. Use DSB to build your own highly customised games.

Moderator: Sophia

Forum rules
Please read the Forum rules and policies before posting.
Post Reply
User avatar
Gambit37
Should eat more pies
Posts: 13715
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Alpha not respected in previously working utility function (0.78)

Post by Gambit37 »

Sorry to be a pain, but I have a new problem in 0.78.

I have a function I use for tinting bitmaps, which I use to create shadows for icons and other special effects. In 0.78, the alpha is no longer respected when I use this function, even though it previously worked.

This is an example of how I might use it:

Code: Select all

dsb_bitmap_draw(bitmap_adjustHSL(icon,0,0,0), bmp, bx+x_off, by+y_off+2, false)
This is the function, is there a modification I can make to get back the previous behaviour? Or do I need to do something different in how I use it?

Code: Select all

function bitmap_adjustHSL(bmp,angle,saturation,lightness)
	local width = dsb_bitmap_width(bmp)
	local height = dsb_bitmap_height(bmp)
	local cx = 0
	local cy = 0
	local count = 0

	local bmp_tinted = dsb_new_bitmap(width,height)

	for cx = 0,width-1 do
		for cy = 0,height-1 do
			count = count + 1
			local rgb, alpha = dsb_get_pixel(bmp, cx, cy)
			r = rgb[1]; g = rgb[2]; b = rgb[3];

			-- Handle alpha / powerpink of current pixel
			if (alpha == nil ) then alpha = 255 end
			-- If powerpink
			if (r == 255 and g == 0 and b == 255 ) then alpha = 0 end

				h,s,l = rgb_to_hsl({r,g,b})

				-- Adjust Hue
				h = (h + angle) / 360
					if(h > 1) then h = h - 1 end

				-- Adjust saturation
				s = s * saturation
					if(s > 1) then s = 1 end
					if(s < 0) then s = 0 end

				-- Adjust lightness
				l = l * lightness
					if(l > 1) then l = 1 end
					if(l < 0) then l = 0 end

				r,g,b = hsl_to_rgb(h,s,l)

			--push new pixel into the bitmap
			dsb_set_pixel(bmp_tinted, cx, cy, {r,g,b}, alpha)
		end
	end
	return bmp_tinted
end
User avatar
Sophia
Concise and Honest
Posts: 4240
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Alpha not respected in previously working utility function (0.78)

Post by Sophia »

You need to dsb_bitmap_clear_alpha the bitmap to assert that you want to use an alpha channel and what the base alpha value for unmodified pixels should be. It worked before because DSB tried to be "smart," but as you saw, sometimes it made the wrong assumptions and everything got messed up. So I changed things so you had to be explicit.

Clearing the bitmap is an extra blit that will slow things down slightly, but, on the other hand, if you're having speed problems, you might want to consider caching the tinted bitmap instead of generating it every time you draw it.
User avatar
Gambit37
Should eat more pies
Posts: 13715
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Alpha not respected in previously working utility function (0.78)

Post by Gambit37 »

Aha, right thanks that makes sense. I'll go back and rework these, and try caching too.
User avatar
Gambit37
Should eat more pies
Posts: 13715
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Alpha not respected in previously working utility function (0.78)

Post by Gambit37 »

Clearing the bitmap to alpha 0 fixed all the issues. I also changed my UI functions to use shadow icons generated at startup, rather than dynamically tinted. Should be more efficient now. :)
Post Reply