【问题标题】:Ideas/Assistance with ActionScript Color Fading?ActionScript 褪色的想法/帮助?
【发布时间】:2012-06-02 11:01:46
【问题描述】:

这是我添加到基于 Flash 的聊天中的一个功能,用于对 [rainbow] 和 [/rainbow] 标记中的某些文本进行彩虹处理。

ChatUI.prototype.rainbowParse = function(txt) {
    txt = txt;
    if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
        txt = txt.replace("'", "@").replace("'", "@");
        var firstChar = txt.indexOf("[rainbow]") + 9;
        var lastChar = txt.indexOf("[/rainbow]");

        if (((lastChar - firstChar) > 100) || ((txt.split("[rainbow]").length - 1) > 3)) {
            break;
        }

        while (lastChar <= txt.lastIndexOf("[/rainbow]")) {
            var RAINBOWTEXT = '';
            var i = firstChar;
            while (i < lastChar) {
                RAINBOWTEXT += txt.charAt(i);
                i++
            }
            var text = RAINBOWTEXT;
            var texty = '';

            colors = new Array('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000','ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00','99ff00','66ff00','33ff00','00ff00','00ff33','00ff66','00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff','0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');

            i = 0;

            while (i <= text.length) {
                var t = text.charAt(i);

                if (t != undefined) {
                    texty += "<font color=\"#" + colors[i % colors.length] + "\">" + t + "</font>";
                    i++;
                }
            }

            texty = texty.replace("> <", ">&nbsp;<");
            var REPLACEME = "[rainbow]" + RAINBOWTEXT + "[/rainbow]";
            txt = txt.replace(REPLACEME, texty);

            if (lastChar == txt.lastIndexOf("[/rainbow]")) {
                break;
            }
            nextChar = lastChar + 10;
            firstChar = txt.indexOf("[rainbow]", lastChar) + 9;
            lastChar = txt.indexOf("[/rainbow]", lastChar);
        }
        txt = txt.replace("@", "&apos;");
    }
    return txt;
}

但是,我不喜欢这些彩虹的样子。文本的颜色会重复。

要查看我的意思的示例,请转到 http://www.tektek.org/color/ 并单击“Rainbow”并在重复设置为 1 的情况下进行预览。然后将其设置为 3 或更高进行预览。

我希望我的代码重复 1,但由于彩虹文本长度变化很大,我不知道该怎么做。我用谷歌搜索了许多彩虹文本生成器,试图查看他们的代码。糟透了。请给我一些想法或帮助。 :(

【问题讨论】:

    标签: javascript actionscript


    【解决方案1】:

    您需要将颜色数组中的元素数除以彩虹字符串中的字符数,然后将每种颜色应用于字符串中的字符数。这样,无论字符串的长度如何,每种颜色都只会以相等的比例应用一次:

    // Calculate the number of characters to apply each character to
    var inc = Math.round(colors.length / txt.length);
    // Empty string to store the modified rainbox text in
    var str = ""; 
    // Loop through each color and apply it to the correct number of characters
    for (var i = 0; i < colors.length; i ++) {
        str += "<font color='#'" + colors[i] + "'>" 
            + txt.substr(i * inc, inc)
            + "</font>";
    }
    

    编辑:

    好的,我重新阅读了问题并再次查看了您链接到的示例,我认为更好的解决方案是使用绘图 API 在 Sprite 中创建线性渐变,并使用包含文本的文本字段对其进行屏蔽必须应用彩虹效果:

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.Sprite;
    import flash.display.GradientType;
    import flash.text.TextFieldAutoSize;
    import flash.geom.Matrix;
    import flash.text.Font;
    
    // You need to embed the font to use it as a mask 
    Font.registerFont(Arial);
    
    var txt:String = "My Rainbow text";
    
    // Removed some of your colors to save time formatting
    var colors:Array = [0xff00ff, 0xff00cc, 0xff0099, 0xff0066, 0xff0033, 
                        0xff0000, 0xff3300, 0xff6600, 0xff9900, 0xffcc00, 
                        0xffff00, 0xccff00, 0x99ff00, 0x66ff00, 0x33ff00];
    
    var alphas:Array = [];
    var ratios:Array = [];
    
    // Populate alphas and ratios arrays of the same length as colors array
    for (var i:int = 0; i < colors.length; i ++)
    {
        alphas.push(1); 
        ratios.push(i * Math.round(255 / colors.length)); // Equal ratio for each color
    }
    
    // Create a text field
    var field:TextField = new TextField();
    field.text = txt;
    field.autoSize = TextFieldAutoSize.LEFT;
    field.setTextFormat(new TextFormat("Arial", 30, 0x0000000));
    field.embedFonts = true;
    
    // Create a gradient of the same dimensions as the text field
    var matrix:Matrix = new Matrix();
    matrix.createGradientBox(field.width, field.height);
    
    var gradient:Sprite = new Sprite();
    gradient.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
    gradient.graphics.drawRect(0, 0, field.width, field.height);
    gradient.graphics.endFill();
    
    this.addChild(field);
    this.addChild(gradient);
    
    // Mask the gradient with the text field
    gradient.mask = field;
    

    【讨论】:

    • 谢谢!我在想类似的事情,但是我不知道如何实现它。如果字符串是奇数会发生什么?我想我可以解决的一种方法是检查字符串的计数,如果它很奇怪,请在字符串的末尾添加一个空格......这可能会起作用。
    • 嗯,我已经将它与我的功能集成。 pastebin.com/raw.php?i=BYrknQXW 是我得到的,我一直在 jsfiddle.net 上测试它以节省编译时间,但我无法让它输出任何东西。
    • 对不起,戴夫,我刚刚更新了我的答案!我认为第二种方法可能更接近你想要的。如果彩虹字符串中的字符少于颜色,则第一个失败。
    猜你喜欢
    • 1970-01-01
    • 2012-10-24
    • 2017-05-03
    • 1970-01-01
    • 2011-08-07
    • 2021-09-03
    • 1970-01-01
    • 2011-03-17
    • 2016-06-25
    相关资源
    最近更新 更多