Important alert: (current site time 7/15/2013 10:49:58 PM EDT)
 

VB icon

Adjust a GIF's Hue/Sat/Lum

Email
Submitted on: 11/17/2003 5:24:56 PM
By: Mark Kahn  
Level: Advanced
User Rating: By 6 Users
Compatibility: ASP (Active Server Pages)
Views: 16622
(About the author)
 
     This code adjusts the Hue, Saturation and Luminosity of any gif file on the fly! No component needed, it's just asp code. live demo: http://www.cwolves.com/testing/coloradj.html Live site using this: http://www.cwolves.com/home.asp?hue=0&sat=0&lum=0&con=0&red=true&green=true&blue=true&invert=false play with all the querystring values in that link.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Adjust a GIF's Hue/Sat/Lum
' Description:This code adjusts the Hue, Saturation and Luminosity of any gif file on the fly! No component needed, it's just asp code.
live demo: 
http://www.cwolves.com/testing/coloradj.html
Live site using this:
http://www.cwolves.com/home.asp?hue=0&sat=0&lum=0&con=0&red=true&green=true&blue=true&invert=false
play with all the querystring values in that link.
' By: Mark Kahn
'
' Inputs:querystring inputs:
img (image path)
hue (-180 --> 180)
sat (-100 --> 100)
lum (-100 --> 100)
'
' Returns:your new gif
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8626&lngWId=4'for details.'**************************************

<%
response.contenttype	= "image/gif"
src = request.querystring("Img")
Hue = request.querystring("Hue")
Sat = request.querystring("Sat")
Lum = request.querystring("Lum")
function dofix(val)
	if val > 239 then
		val = val - 239
		val = dofix(val)
	elseif val < 0 then
		val = val + 239
		val = dofix(val)
	end if
	dofix = val
end function
function dofix2(val)
	if val > 239 then
		dofix2 = 239
	elseif val < 0 then
		dofix2 = 0
	else
		dofix2 = val
	end if
end function
function hexval(char)
	select case char
	case "0": hexval	= 0
	case "1": hexval	= 1
	case "2": hexval	= 2
	case "3": hexval	= 3
	case "4": hexval	= 4
	case "5": hexval	= 5
	case "6": hexval	= 6
	case "7": hexval	= 7
	case "8": hexval	= 8
	case "9": hexval	= 9
	case "A": hexval	= 10
	case "B": hexval	= 11
	case "C": hexval	= 12
	case "D": hexval	= 13
	case "E": hexval	= 14
	case "F": hexval	= 15
	end select
end function
function Ascii(color)
	out	= ""
	for i = 0 to 2
		this	= mid(color, i*2+1, 2)
		tmp	= 16*hexval(left(this, 1)) + hexval(right(this, 1))
		out	= out & chr(tmp)
	next
	Ascii	= out
end function
Function stb(tString)
 Dim I, B
 For I=1 to len(tString)
 B = B & ChrB(Asc(Mid(tString,I,1)))
 Next
 stb = B
End Function
Function RGBtoHSL(ByVal Red, ByVal Green, ByVal Blue, ByRef Hue, ByRef Sat, ByRef Lum)
	pRed = Red / 255
	pGreen = Green / 255
	pBlue = Blue / 255
	If pRed > pGreen Then
		If pRed > pBlue Then pMax = pRed Else pMax = pBlue
	ElseIf pGreen > pBlue Then
		pMax = pGreen
	Else
		pMax = pBlue
	End If
	If pRed < pGreen Then
		If pRed < pBlue Then pMin = pRed Else pMin = pBlue
	ElseIf pGreen < pBlue Then
		pMin = pGreen
	Else
		pMin = pBlue
	End If
	pLum = (pMax + pMin) / 2
	If pMax = pMin Then
		pSat = 0
		pHue = 0
		Else
			If pLum < 0.5 Then
				pSat = (pMax - pMin) / (pMax + pMin)
			Else
				pSat = (pMax - pMin) / (2 - pMax - pMin)
		End If
		Select Case pMax
			Case pRed
				pHue = (pGreen - pBlue) / (pMax - pMin)
			Case pGreen
				pHue = 2 + (pBlue - pRed) / (pMax - pMin)
			Case pBlue
				pHue = 4 + (pRed - pGreen) / (pMax - pMin)
		End Select
	End If
	Hue = pHue * 239 \ 6
	If Hue < 0 Then Hue = Hue + 240
	Sat = Int(pSat * 239)
	Lum = Int(pLum * 239)
End Function
Function HSLtoRGB(ByRef Red, ByRef Green, ByRef Blue, ByVal Hue, ByVal Sat, ByVal Lum)
	Dim temp3()
	ReDim temp3(3)
	pHue = Hue / 239
	pSat = Sat / 239
	pLum = Lum / 239
	If pSat = 0 Then
		pRed = pLum
		pGreen = pLum
		pBlue = pLum
	Else
		If pLum < 0.5 Then
			temp2 = pLum * (1 + pSat)
		Else
			temp2 = pLum + pSat - pLum * pSat
		End If
		temp1 = 2 * pLum - temp2
		temp3(0) = pHue + 1 / 3
		temp3(1) = pHue
		temp3(2) = pHue - 1 / 3
		For n = 0 To 2
			If temp3(n) < 0 Then temp3(n) = temp3(n) + 1
			If temp3(n) > 1 Then temp3(n) = temp3(n) - 1
			If 6 * temp3(n) < 1 Then
				temp3(n) = temp1 + (temp2 - temp1) * 6 * temp3(n)
			Else
				If 2 * temp3(n) < 1 Then
					temp3(n) = temp2
				Else
					If 3 * temp3(n) < 2 Then temp3(n) = temp1 + (temp2 - temp1) * ((2 / 3) - temp3(n)) * 6 Else temp3(n) = temp1
				End If
			End If
		Next
		pRed = temp3(0)
		pGreen = temp3(1)
		pBlue = temp3(2)
	End If
	Red = Int(pRed * 255)
	Green = Int(pGreen * 255)
	Blue = Int(pBlue * 255)
End Function
if not(src	= "" or Hue	= "") then
	set FSO	= server.createobject("Scripting.FileSystemObject")
	set file	= FSO.OpenTextFile(server.mappath(src), 1, false)
	StartData = file.read(13)
	TableSize = asc(mid(StartData, 11, 1))
	tSize= 1
	if (TableSize AND 1) = 1 then tSize = tSize + 1
	if (TableSize AND 2) = 2 then tSize = tSize + 2
	if (TableSize AND 4) = 4 then tSize = tSize + 4
	tSize= 2^tSize
	ColorData = file.read(tSize*3)
	EndData = ""
	do while not file.atendofstream
		EndData = EndData & file.read(100)
	loop
	file.close: set file	= nothing
	set FSO	= nothing
	NewColorData = ""
	for i = 1 to len(ColorData)-2 STEP 3
		R = asc(mid(colordata, i+0, 1))
		G = asc(mid(colordata, i+1, 1))
		B = asc(mid(colordata, i+2, 1))
		H = 0
		L = 0
		S = 0
		call RGBtoHSL(R, G, B, H, S, L)
		H = H + (239/360*Hue)
		if Sat < 0 then S = S + Sat * .01 * (S) else S = S + Sat * .01 * (239-S)
		if Lum < 0 then L = L + Lum * .01 * (L) else L = L + Lum * .01 * (239-L)
		h = dofix(h)
		s = dofix2(s)
		l = dofix2(l)
		call HSLtoRGB(R, G, B, H, S, L)
		NewColorData = NewColorData & chr(R) & chr(G) & chr(B)
	next
	response.binarywrite stb(StartData & NewColorData & EndData)
end if
%>


Other 19 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/18/2003 3:23:47 AM

Great code!

All we need is the ability to resize the gif file. This it is perfect...

;)Lars
(If this comment was disrespectful, please report it.)

 
11/18/2003 9:11:43 AMMark Kahn

I'm working on the resize one...since the lzw patent expired and I'm allowed to now :-)

Hopefully within a few months I'll have a full-featured asp-only gif editor. hehe.
(If this comment was disrespectful, please report it.)

 
11/24/2003 3:30:33 AM

Awesome code. Now, how about the RGB options like in your example?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.