20 Мая 2024, 09:39:30
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.

Навигация по форуму







guest3d
Quest3D - Русскоязычное сообщество > Quest3D > База исходников (cgr/igr) > color blending
color blending
(Прочитано 9293 раз)
  [1]
Печать
Const_47 | **** | Старожил | Сообщений: 299 | « 01 Марта 2009, 19:24:28 »
color blending |
0
Набор готовых макросов для HLSL. Фотожоповские эффекты смешивания цветов. Там пишет откуда стыбрано:))) Мне очень пригодилось.
Const_47 | **** | Старожил | Сообщений: 299 | «Ответ #1 01 Марта 2009, 19:24:59 »
Re: color blending |
1
/*
** Photoshop & misc math
** Blending modes, RGB/HSL/Contrast/Desaturate
**
** Romain Dura | Romz
** Blog: http://blog.mouaif.org
** Post: http://blog.mouaif.org/?p=94
*/


/*
** Desaturation
*/

float4 Desaturate(float3 color, float Desaturation)
{
   float3 grayXfer = float3(0.3, 0.59, 0.11);
   float grayf = dot(grayXfer, color);
   float3 gray = float3(grayf, grayf, grayf);
   return float4(lerp(color, gray, Desaturation), 1.0);
}


/*
** Hue, saturation, luminance
*/

float3 RGBToHSL(float3 color)
{
   float3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part)
   
   float fmin = min(min(color.r, color.g), color.b);    //Min. value of RGB
   float fmax = max(max(color.r, color.g), color.b);    //Max. value of RGB
   float delta = fmax - fmin;             //Delta RGB value

   hsl.z = (fmax + fmin) / 2.0; // Luminance

   if (delta == 0.0)      //This is a gray, no chroma...
   {
      hsl.x = 0.0;   // Hue
      hsl.y = 0.0;   // Saturation
   }
   else                                    //Chromatic data...
   {
      if (hsl.z < 0.5)
         hsl.y = delta / (fmax + fmin); // Saturation
      else
         hsl.y = delta / (2.0 - fmax - fmin); // Saturation
      
      float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
      float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
      float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;

      if (color.r == fmax )
         hsl.x = deltaB - deltaG; // Hue
      else if (color.g == fmax)
         hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue
      else if (color.b == fmax)
         hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue

      if (hsl.x < 0.0)
         hsl.x += 1.0; // Hue
      else if (hsl.x > 1.0)
         hsl.x -= 1.0; // Hue
   }

   return hsl;
}

float HueToRGB(float f1, float f2, float hue)
{
   if (hue < 0.0)
      hue += 1.0;
   else if (hue > 1.0)
      hue -= 1.0;
   float res;
   if ((6.0 * hue) < 1.0)
      res = f1 + (f2 - f1) * 6.0 * hue;
   else if ((2.0 * hue) < 1.0)
      res = f2;
   else if ((3.0 * hue) < 2.0)
      res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
   else
      res = f1;
   return res;
}

float3 HSLToRGB(float3 hsl)
{
   float3 rgb;
   
   if (hsl.y == 0.0)
      rgb = float3(hsl.z, hsl.z, hsl.z); // Luminance
   else
   {
      float f2;
      
      if (hsl.z < 0.5)
         f2 = hsl.z * (1.0 + hsl.y);
      else
         f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
         
      float f1 = 2.0 * hsl.z - f2;
      
      rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
      rgb.g = HueToRGB(f1, f2, hsl.x);
      rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
   }
   
   return rgb;
}

/*
** Contrast, saturation, brightness
** Code of this function is from TGM's shader pack
** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057
*/

// For all settings: 1.0 = 100% 0.5=50% 1.5 = 150%
float3 ContrastSaturationBrightness(float3 color, float brt, float sat, float con)
{
   // Increase or decrease theese values to adjust r, g and b color channels seperately
   const float AvgLumR = 0.5;
   const float AvgLumG = 0.5;
   const float AvgLumB = 0.5;
   
   const float3 LumCoeff = float3(0.2125, 0.7154, 0.0721);
   
   float3 AvgLumin = float3(AvgLumR, AvgLumG, AvgLumB);
   float3 brtColor = color * brt;
   float intensityf = dot(brtColor, LumCoeff);
   float3 intensity = float3(intensityf, intensityf, intensityf);
   float3 satColor = lerp(intensity, brtColor, sat);
   float3 conColor = lerp(AvgLumin, satColor, con);
   return conColor;
}

/*
** Float blending modes
** Adapted from here: http://www.nathanm.com/photoshop-blending-math/
** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results
*/

#define BlendLinearDodgef          BlendAddf
#define BlendLinearBurnf          BlendSubstractf
#define BlendAddf(base, blend)       min(base + blend, 1.0)
#define BlendSubstractf(base, blend)    max(base + blend - 1.0, 0.0)
#define BlendLightenf(base, blend)       max(blend, base)
#define BlendDarkenf(base, blend)       min(blend, base)
#define BlendLinearLightf(base, blend)    (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
#define BlendScreenf(base, blend)       (1.0 - ((1.0 - base) * (1.0 - blend)))
#define BlendOverlayf(base, blend)    (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
#define BlendSoftLightf(base, blend)    ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend)))
#define BlendColorDodgef(base, blend)    ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0))
#define BlendColorBurnf(base, blend)    ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0))
#define BlendVividLightf(base, blend)    ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5))))
#define BlendPinLightf(base, blend)    ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5))))
#define BlendHardMixf(base, blend)    ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0)
#define BlendReflectf(base, blend)       ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0))



/*
** Vector3 blending modes
*/

// Component wise blending
#define Blend(base, blend, funcf)       float3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b))

#define BlendNormal(base, blend)       (base)
#define BlendLighten            BlendLightenf
#define BlendDarken            BlendDarkenf
#define BlendMultiply(base, blend)       (base * blend)
#define BlendAverage(base, blend)       ((base + blend) / 2.0)
#define BlendAdd(base, blend)       min(base + blend, float3(1.0, 1.0, 1.0))
#define BlendSubstract(base, blend)    max(base + blend - float3(1.0, 1.0, 1.0), float3(0.0, 0.0, 0.0))
#define BlendDifference(base, blend)    abs(base - blend)
#define BlendNegation(base, blend)    (float3(1.0, 1.0, 1.0) - abs(float3(1.0, 1.0, 1.0) - base - blend))
#define BlendExclusion(base, blend)    (base + blend - 2.0 * base * blend)
#define BlendScreen(base, blend)       Blend(base, blend, BlendScreenf)
#define BlendOverlay(base, blend)       Blend(base, blend, BlendOverlayf)
#define BlendSoftLight(base, blend)    Blend(base, blend, BlendSoftLightf)
#define BlendHardLight(base, blend)    BlendOverlay(blend, base)
#define BlendColorDodge(base, blend)    Blend(base, blend, BlendColorDodgef)
#define BlendColorBurn(base, blend)    Blend(base, blend, BlendColorBurnf)
#define BlendLinearDodge         BlendAdd
#define BlendLinearBurn         BlendSubstract

// Linear Light is another contrast-increasing mode
// If the blend color is darker than midgray, Linear Light darkens the image by decreasing the brightness. If the blend color is lighter than midgray, the result is a brighter image due to increased brightness.
#define BlendLinearLight(base, blend)    Blend(base, blend, BlendLinearLightf)

#define BlendVividLight(base, blend)    Blend(base, blend, BlendVividLightf)
#define BlendPinLight(base, blend)       Blend(base, blend, BlendPinLightf)
#define BlendHardMix(base, blend)       Blend(base, blend, BlendHardMixf)
#define BlendReflect(base, blend)       Blend(base, blend, BlendReflectf)
#define BlendGlow(base, blend)       BlendReflect(blend, base)
#define BlendPhoenix(base, blend)       (min(base, blend) - max(base, blend) + float3(1.0, 1.0, 1.0))
#define BlendOpacity(base, blend, F, O)    (F(base, blend) * O + blend * (1.0 - O))

// Hue Blend mode creates the result color by combining the luminance and saturation of the base color with the hue of the blend color.
float3 BlendHue(float3 base, float3 blend)
{
   float3 baseHSL = RGBToHSL(base);
   return HSLToRGB(float3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b));
}

// Saturation Blend mode creates the result color by combining the luminance and hue of the base color with the saturation of the blend color.
float3 BlendSaturation(float3 base, float3 blend)
{
   float3 baseHSL = RGBToHSL(base);
   return HSLToRGB(float3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b));
}

// Color Mode keeps the brightness of the base color and applies both the hue and saturation of the blend color.
float3 BlendColor(float3 base, float3 blend)
{
   float3 blendHSL = RGBToHSL(blend);
   return HSLToRGB(float3(blendHSL.r, blendHSL.g, RGBToHSL(base).b));
}

// Luminosity Blend mode creates the result color by combining the hue and saturation of the base color with the luminance of the blend color.
float3 BlendLuminosity(float3 base, float3 blend)
{
   float3 baseHSL = RGBToHSL(base);
   return HSLToRGB(float3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b));
}
→|๖ۣۜDen|← | ***** | Администратор | Ветеран | Сообщений: 570 | «Ответ #2 01 Марта 2009, 19:51:04 »
Re: color blending |
2
Можно exe или скрины, как это выглядит
Const_47 | **** | Старожил | Сообщений: 299 | «Ответ #3 01 Марта 2009, 20:57:53 »
Re: color blending |
3
&Den
Открой фотошоп и посмотри как это выглядит Улыбающийся))
Это те же, или почти те алгоритмы которые используются при смешивании слоев в фотошопе.
С помощью некоторых создал вот такой элемент управления
http://rapidshare.com/files/204080898/colortest.rar.html
Я кстати вкурсе что он кривой и не доработанный. Это младенец
Улыбающийся)) пол часа отроду.
→|๖ۣۜDen|← | ***** | Администратор | Ветеран | Сообщений: 570 | «Ответ #4 01 Марта 2009, 21:22:38 »
Re: color blending |
4
а)) палитра то бишь) Улыбающийся

* colortest.JPG (16.56 Кб, 711x584 - просмотрено 1290 раз.)
Const_47 | **** | Старожил | Сообщений: 299 | «Ответ #5 02 Марта 2009, 03:04:29 »
Re: color blending |
5
Неа Ден, это у меня палитра. А то что выше это именно алгоритмы  ColorBlending в hlsl
Вот же они в фотошопе
http://img129.imageshack.us/my.php?image=colorblending.jpg
→|๖ۣۜDen|← | ***** | Администратор | Ветеран | Сообщений: 570 | «Ответ #6 02 Марта 2009, 03:31:50 »
Re: color blending |
6
ну ясно Веселый
IYV | ** | Пользователь | Сообщений: 77 | «Ответ #7 23 Ноября 2009, 00:39:47 »
Re: color blending |
7
Const_47,а как применять эти макросы,их нужно в пиксельный блок вписывать?С помощью них можно реализовать эффект,что достигается перемещением ползунка Hue,в Hue/Saturation в фотошопе,как на картинке ниже?

         

* 01.jpg (75.35 Кб, 1220x554 - просмотрено 1266 раз.)
 
  [1]
Печать
 
Quest3D - Русскоязычное сообщество > Quest3D > База исходников (cgr/igr) > color blending
Перейти в: