HLSL色调变化算法

时间:2011-01-18 17:04:07

标签: silverlight algorithm pixel shader hlsl

我想为Silverlight制作一个像素着色器,这将帮助我使用滑块更改色相/饱和度/亮度。 * Hue滑块的值范围为:[ - 180,180] *饱和度滑块的值范围为:[ - 100,100] *亮度滑块的值范围为:[ - 100,100]

我设法创建了一个可以操纵饱和度和亮度值的像素着色器。 但我可以找到任何改变色调值的算法。 任何人都可以提供算法吗?谢谢。

这是我的HLSL代码:

/// <summary>The brightness offset.</summary>
/// <minValue>-180</minValue>
/// <maxValue>180</maxValue>
/// <defaultValue>0</defaultValue>
float Hue : register(C0);

/// <summary>The saturation offset.</summary>
/// <minValue>-100</minValue>
/// <maxValue>100</maxValue>
/// <defaultValue>0</defaultValue>
float Saturation : register(C1);

/// <summary>The lightness offset.</summary>
/// <minValue>-100</minValue>
/// <maxValue>100</maxValue>
/// <defaultValue>0</defaultValue>
float Lightness : register(C2);

sampler2D input : register(S0);

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------

float4 main(float2 uv : TEXCOORD) : COLOR
{
 // some vars
 float saturation = Saturation / 100 + 1;
 float lightness = Lightness / 100;
 float3  luminanceWeights = float3(0.299,0.587,0.114);

    // input raw pixel
    float4 srcPixel = tex2D(input, uv);

    // Apply saturation
    float luminance = dot(srcPixel, luminanceWeights);
    float4 dstPixel = lerp(luminance, srcPixel, saturation);

    // Apply lightness
    dstPixel.rgb += lightness;

    //retain the incoming alpha
    dstPixel.a = srcPixel.a;

    return dstPixel;
}

3 个答案:

答案 0 :(得分:1)

EasyRGB提供了颜色空间之间的转换,请参阅nokola.com上的这篇文章,了解Hue转换的Silverlight实现。如果你采用here提到的方法但是我没有尝试过,可以在一个PS 2.0着色器中拟合色调,饱和度和亮度。

答案 1 :(得分:1)

输入域略有不同,但可以轻松调整:

float Hue : register(C0); // 0..360, default 0
float Saturation : register(C1); // 0..2, default 1
float Luminosity : register(C2); // -1..1, default 0

sampler2D input1 : register(S0);

static float3x3 matrixH =
{
  0.8164966f,  0,           0.5352037f,
 -0.4082483f,  0.70710677f, 1.0548190f,
 -0.4082483f, -0.70710677f, 0.1420281f
};

static float3x3 matrixH2 =
{
  0.84630f, -0.37844f, -0.37844f,
 -0.37265f,  0.33446f, -1.07975f,
  0.57735f,  0.57735f,  0.57735f
};

float4 main(float2 uv : TEXCOORD) : COLOR
{
  float4 c = tex2D(input1, uv);

  float3x3 rotateZ =
  {
    cos(radians(Hue)), sin(radians(Hue)), 0,
   -sin(radians(Hue)), cos(radians(Hue)), 0,
    0,                 0,                 1
  };
  matrixH = mul(matrixH, rotateZ);
  matrixH = mul(matrixH, matrixH2);

  float i = 1 - Saturation;
  float3x3 matrixS =
  {
    i*0.3086f+Saturation, i*0.3086f,            i*0.3086f,
    i*0.6094f,            i*0.6094f+Saturation, i*0.6094f,
    i*0.0820f,            i*0.0820f,            i*0.0820f+Saturation
  };
  matrixH = mul(matrixH, matrixS);

  float3 c1 = mul(c, matrixH);
  c1 += Luminosity;
  return float4(c1, c.a);
}

答案 2 :(得分:-1)