Skip to content

Commit 26d09e0

Browse files
HSV <-> RGB converter
1 parent 15371ef commit 26d09e0

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Diff for: hsv.shader

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Shader "HSV"
2+
{
3+
Properties
4+
{
5+
_MainTex ("Texture Map", 2D) = "white" {}
6+
_hue ("Hue",Range(0.0,1.0)) = 1.0
7+
_saturation ("Saturation",Range(0.0,1.0)) = 1.0
8+
_value ("Value (Brightness)",Range(0.0,1.0)) = 1.0
9+
}
10+
SubShader
11+
{
12+
Pass
13+
{
14+
CGPROGRAM
15+
#pragma vertex SetVertexShader
16+
#pragma fragment SetPixelShader
17+
18+
sampler2D _MainTex;
19+
float4 _MainTex_ST;
20+
float _hue, _saturation, _value;
21+
22+
float3 RGBToHSV(float3 c)
23+
{
24+
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
25+
float4 p = lerp( float4( c.bg, K.wz ), float4( c.gb, K.xy ), step( c.b, c.g ) );
26+
float4 q = lerp( float4( p.xyw, c.r ), float4( c.r, p.yzx ), step( p.x, c.r ) );
27+
float d = q.x - min( q.w, q.y );
28+
float e = 1.0e-10;
29+
return float3( abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
30+
}
31+
32+
float3 HSVToRGB( float3 c )
33+
{
34+
float4 K = float4( 1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0 );
35+
float3 p = abs( frac( c.xxx + K.xyz ) * 6.0 - K.www );
36+
return c.z * lerp( K.xxx, saturate( p - K.xxx ), c.y );
37+
}
38+
39+
void SetVertexShader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0)
40+
{
41+
vertex = UnityObjectToClipPos(vertex);
42+
}
43+
44+
float4 SetPixelShader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : SV_TARGET
45+
{
46+
float4 color = tex2D(_MainTex, uv * _MainTex_ST.xy + _MainTex_ST.zw);
47+
float3 source = RGBToHSV(color.rgb);
48+
source *= float3(_hue, _saturation, _value);
49+
color.rgb = HSVToRGB(source);
50+
return color;
51+
}
52+
53+
ENDCG
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)