ColorKeyer - 图1

    1. /*
    2. * colorKeyer.osl by Fabricio Chamon
    3. * Created for Redshift
    4. * Date Mar-27-2021
    5. * http://www.patreon.com/fchamon
    6. * This file is licensed under Apache 2.0 license
    7. */
    8. float changeRange(float val, float from_min, float from_max, float to_min, float to_max){
    9. return clamp(to_min + (val - from_min) * (to_max - to_min) / (from_max - from_min), 0, 1);
    10. }
    11. shader ColorKeyer
    12. [[ string help = "Keys Specific Colors",
    13. string label = "Color Keyer" ]]
    14. (
    15. int Bypass = 0
    16. [[string widget = "checkBox"]],
    17. color Texture = color(0,0,0),
    18. color ColorToExtract = color(1,0,0),
    19. float HueTolerance = 0.01
    20. [[float min=0, float max=1]],
    21. float SatTolerance = 0.0
    22. [[float min=0, float max=1]],
    23. float ValTolerance = 1.0
    24. [[float min=0, float max=1]],
    25. int InvertMatte = 0
    26. [[string widget = "checkBox"]],
    27. float Contrast = 0
    28. [[float min=0, float max=1]],
    29. float Factor = 1
    30. [[float min=0, float max=10]],
    31. output color Matte = color(0,0,0)
    32. )
    33. {
    34. float errorMargin = 0.0001;
    35. color TextureHSV = transformc("hsv", Texture);
    36. color ColorHSV = transformc("hsv", ColorToExtract);
    37. float hue = changeRange(abs(TextureHSV[0]-ColorHSV[0]), 0, HueTolerance+errorMargin, 1, 0);
    38. float sat = changeRange(abs(TextureHSV[1]-ColorHSV[1]), 0, SatTolerance+errorMargin, 1, 0);
    39. float val = changeRange(abs(TextureHSV[2]-ColorHSV[2]), 0, ValTolerance+errorMargin, 1, 0);
    40. float mask = hue*sat*val;
    41. float contrast_remapped = changeRange(Contrast, 0, 1, 0.5, 0);
    42. float contrast_min = 0.5-contrast_remapped;
    43. float contrast_max = 0.5+contrast_remapped;
    44. float contrast_mask = changeRange(mask, contrast_min, contrast_max, 0, 1);
    45. mask = contrast_mask * Factor;
    46. if (InvertMatte){mask = 1-mask;}
    47. Matte = (Bypass)?Texture:mask;
    48. }