代码之家  ›  专栏  ›  技术社区  ›  BladePoint

AS3:手动计算RGB乘数和亮度从-1到1的偏移

  •  1
  • BladePoint  · 技术社区  · 7 年前

    但是,如何计算大于0的亮度值的偏移?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Organis    7 年前

    这是简单的每通道插值数学。它看起来并不简单,因为有三个通道,它们需要用于各种目的的反序列化。

    // Usage.
    
    acoolor:uint = parseRGB(200, 150, 100);
    
    trace(colorToRGB(brightNess(acoolor, 0.5)));
    trace(colorToRGB(brightNess(acoolor, -0.5)));
    
    // Implementation.
    
    function parseRGB(ared:uint, agreen:uint, ablue:uint):uint
    {
        var result:uint;
    
        result += (ared << 16) & 0xFF0000;
        result += (agreen << 8) & 0xFF00;
        result += (ablue) & 0xFF;
    
        return result;
    }
    
    function colorToRGB(acolor:uint):Array
    {
        result = new Array;
    
        result[0] = (acolor >> 16) & 0xFF;
        result[1] = (acolor >> 8) & 0xFF;
        result[2] = (acolor) & 0xFF;
    
        return result;
    }
    
    function RGBToColor(anrgb:Array):uint
    {
        return parseRGB.apply(this, anrgb);
    }
    
    function brightChannel(achannel:uint, adjust:Number):uint
    {
        if (adjust <= -1) return 0;
        if (adjust >= 1) return 255;
        if (adjust < 0) return achannel * (1 + adjust);
        if (adjust > 0) return achannel + (255 - achannel) * adjust;
    
        // If adjust == 0
        return achannel;
    }
    
    function brightNess(acolor:uint, adjust:Number):uint
    {
        var anrgb:Array = colorToRGB(acolor);
    
        for (var i:int = 0; i < 3; i++)
            anrgb[i] = brightChannel(anrgb[i], adjust);
    
        return RGBToColor(anrgb);
    }