Increase Saturation

Just starting out? Need help? Post your questions and find answers here.
Primus1
User
User
Posts: 17
Joined: Wed Jan 12, 2011 2:48 pm
Contact:

Increase Saturation

Post by Primus1 »

Is there a simple way to increase saturation of a pixel?
Something like:

convert RGB>HSL
S+10 (what would be the max?)
convert HSL>RGB

I've seen some code on the forum but it contains more then I need or is to complex for me and my program :P .
A sketch tool with a twist, SECret INSPiration made using Purebasic.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Increase Saturation

Post by STARGÅTE »

here some code from me:

Code: Select all

 
Procedure MaxColorValue(Value1=0, Value2=0, Value3=0, Value4=0)
	Protected MaxValue = 0
	If Value1 > MaxValue : MaxValue = Value1 : EndIf
	If Value2 > MaxValue : MaxValue = Value2 : EndIf
	If Value3 > MaxValue : MaxValue = Value3 : EndIf
	If Value4 > MaxValue : MaxValue = Value4 : EndIf 
	ProcedureReturn MaxValue
EndProcedure

Procedure MinColorValue(Value1=255, Value2=255, Value3=255, Value4=255)
	Protected MinValue = 255
	If Value1 < MinValue : MinValue = Value1 : EndIf
	If Value2 < MinValue : MinValue = Value2 : EndIf
	If Value3 < MinValue : MinValue = Value3 : EndIf
	If Value4 < MinValue : MinValue = Value4 : EndIf 
	ProcedureReturn MinValue
EndProcedure
 
Procedure.l ColorHSV(Hue.f, Saturation.f, Value.f)
	Protected H.i = Int(Hue/60)
	Protected f.f = (Hue/60-H)
	Protected S.f = Saturation/100
	If S > 1.0 : S = 1.0 : ElseIf S < 0.0 : S = 0.0 : EndIf
	Protected V.f = Value * 2.55
	If V > 255 : V = 255 : ElseIf V < 0 : V = 0 : EndIf
	Protected p.i = V * (1-S)
	Protected q.i = V * (1-S*f)
	Protected t.i = V * (1-S*(1-f))
	Select H
		Case 1 : ProcedureReturn RGB(q,V,p)
		Case 2 : ProcedureReturn RGB(p,V,t)
		Case 3 : ProcedureReturn RGB(p,q,V)  
		Case 4 : ProcedureReturn RGB(t,p,V)
		Case 5 : ProcedureReturn RGB(V,p,q)  
		Default : ProcedureReturn RGB(V,t,p)
	EndSelect
EndProcedure

Procedure.f GetColorSaturation(Color.l)
	Protected Max.i = MaxColorValue(Red(Color),Green(Color),Blue(Color))
	Protected Min.i = MinColorValue(Red(Color),Green(Color),Blue(Color))
	If Max = 0
		ProcedureReturn 0
	Else
		ProcedureReturn 100*(Max-Min)/Max
	EndIf
EndProcedure

Procedure.f GetColorValue(Color.l)
	Protected Max.i = MaxColorValue(Red(Color),Green(Color),Blue(Color))
	ProcedureReturn 100*Max/255
EndProcedure
 
Procedure.f GetColorHue(Color.l)
	Protected Max.i = MaxColorValue(Red(Color),Green(Color),Blue(Color))
	Protected Min.i = MinColorValue(Red(Color),Green(Color),Blue(Color))
	If Max = Min
		ProcedureReturn 0
	ElseIf Max = Red(Color)
		Hue.f = 60.0*(  (Green(Color)-Blue(Color))/(Max-Min))
		If Hue < 0 : Hue + 360 : EndIf
		ProcedureReturn Hue
	ElseIf Max = Green(Color)
		ProcedureReturn 60.0*(2+(Blue(Color)-Red(Color))/(Max-Min))
	Else  
		ProcedureReturn 60.0*(4+(Red(Color)-Green(Color))/(Max-Min))
	EndIf
EndProcedure

Procedure.l SetColorSaturation(Color.l, Saturation.f, Mode.i=#PB_Absolute)
	If Mode = #PB_Absolute 
		ProcedureReturn ColorHSV(GetColorHue(Color), Saturation, GetColorValue(Color))
	Else
		If GetColorSaturation(Color)
			ProcedureReturn ColorHSV(GetColorHue(Color), GetColorSaturation(Color)+Saturation, GetColorValue(Color))
		Else
			ProcedureReturn ColorHSV(GetColorHue(Color), 0, GetColorValue(Color))
		EndIf
	EndIf
EndProcedure

Procedure.f SetColorValue(Color.l, Value.f, Mode.i=#PB_Absolute)
	If Mode = #PB_Absolute
		ProcedureReturn ColorHSV(GetColorHue(Color), GetColorSaturation(Color), Value)
	Else
		ProcedureReturn ColorHSV(GetColorHue(Color), GetColorSaturation(Color), GetColorValue(Color)+Value)
	EndIf
EndProcedure

Procedure.f SetColorHue(Color.l, Hue.f, Mode.i=#PB_Absolute)
	If Mode = #PB_Absolute
		ProcedureReturn ColorHSV(Hue, GetColorSaturation(Color), GetColorValue(Color))
	Else
		ProcedureReturn ColorHSV(GetColorHue(Color)+Hue, GetColorSaturation(Color), GetColorValue(Color))
	EndIf
EndProcedure


Debug RSet(Hex(SetColorHue($FF0000, 90, #PB_Relative)),6,"0")
Debug RSet(Hex(SetColorSaturation($FF0000, -50, #PB_Relative)),6,"0")
Debug RSet(Hex(SetColorValue($FF0000, -50, #PB_Relative)),6,"0")

; your example:
Debug RSet(Hex(SetColorSaturation($80E080, 20, #PB_Relative)),6,"0")
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply