zoukankan      html  css  js  c++  java
  • Unity shader 之 PhotoShop的色相,饱和度,明度算法实现

    效果如下:

    代码如下:

     1 float3 RGBToHSV(float3 rgb)
     2 {
     3    float R = rgb.x;
     4    float G = rgb.y;
     5    float B = rgb.z;
     6    float3 hsv;
     7    float max1 = max(R, max(G, B));
     8    float min1 = min(R, min(G, B));
     9 
    10    if (R == max1)
    11    {
    12        hsv.x = (G - B) / (max1 - min1);
    13    }
    14    if (G == max1)
    15    {
    16        hsv.x = 2.0 + (B - R) / (max1 - min1);
    17    }
    18    if (B == max1)
    19    {
    20        hsv.x = 4.0 + (R - G) / (max1 - min1);
    21    }
    22 
    23    hsv.x = hsv.x * 60.0;
    24    if (hsv.x  < 0.0)
    25    {
    26        hsv.x = hsv.x + 360.0;
    27    }
    28    hsv.z = max1;
    29    hsv.y = (max1 - min1) / max1;
    30    return hsv;
    31 }
    32 
    33 float3 HSVToRGB(float3 hsv)
    34 {
    35    float R;
    36    float G;
    37    float B;
    38    if (hsv.y == 0.0)
    39    {
    40         R = G = B = hsv.z;
    41         return float3(R, G, B);
    42     }
    43 
    44     hsv.x = hsv.x / 60.0;
    45     int i = int(hsv.x);
    46     float f = hsv.x - float(i);
    47     float a = hsv.z * (1.0 - hsv.y);
    48     float b = hsv.z * (1.0 - hsv.y * f);
    49     float c = hsv.z * (1.0 - hsv.y * (1.0 - f));
    50     if (i == 0)
    51     {
    52         R = hsv.z;
    53         G = c;
    54         B = a;
    55     }
    56     else if (i == 1)
    57     {
    58         R = b;
    59         G = hsv.z;
    60         B = a;
    61     }
    62     else if (i == 2)
    63     {
    64         R = a;
    65         G = hsv.z;
    66         B = c;
    67     }
    68     else if (i == 3)
    69     {
    70         R = a;
    71         G = b;
    72         B = hsv.z;
    73     }
    74     else if (i == 4)
    75     {
    76         R = c;
    77         G = a;
    78         B = hsv.z;
    79     }
    80     else
    81     {
    82         R = hsv.z;
    83         G = a;
    84         B = b;
    85     }
    86     return float3(R, G, B);
    87 }
    88 
    89 // 根据色相,饱和度,明度,计算染色颜色
    90 half3 ComputeDyeColor(half4 c, half4 dyeValue)
    91 {
    92     float3 hsv = RGBToHSV(c.rgb);
    93     hsv.x += dyeValue.x;
    94     hsv.x = hsv.x % 360;
    95     hsv.y *= dyeValue.y;
    96     hsv.z *= dyeValue.z;
    97     return HSVToRGB(hsv);
    98 }

    转载请注明出处:https://www.cnblogs.com/jietian331/p/14379175.html

    参考地址:http://imgtec.eetrend.com/d6-imgtec/blog/2016-05/7968.html

  • 相关阅读:
    JavaScript
    94.Binary Tree Inorder Traversal
    144.Binary Tree Preorder Traversal
    106.Construct Binary Tree from Inorder and Postorder Traversal
    105.Construct Binary Tree from Preorder and Inorder Traversal
    90.Subsets II
    78.Subsets
    83.Merge Sorted Array
    80.Remove Duplicates from Sorted Array II
    79.Word Search
  • 原文地址:https://www.cnblogs.com/jietian331/p/14379175.html
Copyright © 2011-2022 走看看