It is currently April 19th, 2024, 7:36 pm

Change Brightness / Invert Brightness

Tips and Tricks from the Rainmeter Community
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Change Brightness / Invert Brightness

Post by jsmorley »

This demonstrates how you can use a single Lua script to modify the "brightness" of a color in several ways.

What it does is take an RGB color in the form rrr,ggg,bbb and converts it to HSB (Hue Saturation Brightness). It then changes the "B" or brightness, and converts this HSB color back to RGB to use in the skin.
ChangeBrightness_1.1.rmskin
There are three skins included in the .rmskin:

1) ChangeBrightness
This will allow you to dynamically change the brightness of a color "darker" or "lighter" by a percentage, while maintaining the Hue and Saturation of the original color.
GIF.gif
2) GradientBrightness
This just demonstrates how you might use the Lua to create various gradients of the original color for use in image or string meters.
2.png
3) InvertBrightness
This finds the inverse of "B". In other words, if the brightness is 20%, then the inverse of that would be 80%. If the brightness is 75%, the inverse of that would be 25%. It then applies that new brightness value to the Hue and Saturation, and converts the value back to RGB to be used in the skin. This uses the included Chameleon plugin to get the colors from your desktop wallpaper.
1.png
roo.png
The two left-hand blocks are the "Dark1" and "Light1" values from the background image, and the two right-hand blocks are the inverse of those colors. Note that this will be less than impressive or even useful if the original color has a "brightness" that is around 50%. The inverse of 49% is 51%, and that isn't much of a change.

--------------------

The somewhat involved formula(s) in the Lua are based on this article:

http://www.easyrgb.com/index.php?X=MATH&H=20#text20

The ChangeBrightness.lua:

Code: Select all

function Update()

	inColor = SELF:GetOption('InColor')

	hue, saturation, brightness = RGBtoHSB(inColor)

	changeValue = SELF:GetOption('Change')
	
	if string.upper(changeValue) == 'INVERT' then
		brightness = 1 - brightness
	else
		brightness = brightness + (changeValue / 100)
	end
	
	red, green, blue = HSBtoRGB(hue, saturation, brightness)
	
	return red..','..green..','..blue
	
end

function RGBtoHSB(colorArg)
	
	inRed, inGreen, inBlue = string.match(colorArg, '(%d+),(%d+),(%d+)')
	
	percentR = ( inRed / 255 )
	percentG = ( inGreen / 255 )
	percentB = ( inBlue / 255 )

	colorMin = math.min( percentR, percentG, percentB )
	colorMax = math.max( percentR, percentG, percentB )
	deltaMax = colorMax - colorMin

	colorBrightness = colorMax

	if (deltaMax == 0) then
		colorHue = 0
		colorSaturation = 0
	else
		colorSaturation = deltaMax / colorMax

		deltaR = (((colorMax - percentR) / 6) + (deltaMax / 2)) / deltaMax
		deltaG = (((colorMax - percentG) / 6) + (deltaMax / 2)) / deltaMax
		deltaB = (((colorMax - percentB) / 6) + (deltaMax / 2)) / deltaMax

		if (percentR == colorMax) then
			colorHue = deltaB - deltaG
		elseif (percentG == colorMax) then 
			colorHue = ( 1 / 3 ) + deltaR - deltaB
		elseif (percentB == colorMax) then 
			colorHue = ( 2 / 3 ) + deltaG - deltaR
		end

		if ( colorHue < 0 ) then colorHue = colorHue + 1 end
		if ( colorHue > 1 ) then colorHue = colorHue - 1 end
		
	end

	return colorHue, colorSaturation, colorBrightness

end

function HSBtoRGB(colorHue, colorSaturation, colorBrightness)
	
		degreesHue = colorHue * 6
		if (degreesHue == 6) then degreesHue = 0 end
		degreesHue_int = math.floor(degreesHue)
		percentSaturation1 = colorBrightness * (1 - colorSaturation)
		percentSaturation2 = colorBrightness * (1 - colorSaturation * (degreesHue - degreesHue_int))
		percentSaturation3 = colorBrightness * (1 - colorSaturation * (1 - (degreesHue - degreesHue_int)))
		if (degreesHue_int == 0)  then
			percentR = colorBrightness
			percentG = percentSaturation3
			percentB = percentSaturation1
		elseif (degreesHue_int == 1) then
			percentR = percentSaturation2
			percentG = colorBrightness
			percentB = percentSaturation1
		elseif (degreesHue_int == 2) then
			percentR = percentSaturation1
			percentG = colorBrightness
			percentB = percentSaturation3
		elseif (degreesHue_int == 3) then
			percentR = percentSaturation1
			percentG = percentSaturation2
			percentB = colorBrightness
		elseif (degreesHue_int == 4) then
			percentR = percentSaturation3
			percentG = percentSaturation1
			percentB = colorBrightness
		else
			percentR = colorBrightness
			percentG = percentSaturation1
			percentB = percentSaturation2
		end
 
		outRed = math.floor(percentR * 255)
		outGreen = math.floor(percentG * 255)
		outBlue = math.floor(percentB * 255)
		
		if (outRed < 0 or outGreen < 0 or outBlue < 0)
		or (outRed > 255 or outGreen > 255 or outBlue > 255) then
			return inRed, inGreen, inBlue
		else
			return outRed, outGreen, outBlue
		end
	
end
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Change Brightness / Invert Brightness

Post by jsmorley »

Updated to use a single Lua script for all the skin variants.