苹果酱推子


11

过去,所有1337个孩子在聊天室中都使用了文本推子。我不了解你们,但是我想像他们一样感到很酷。唯一的问题是,它们的旧脚本和应用程序与所创建的软件紧密耦合,因此我不能随便在任何地方使用这一惊人的功能。我还希望该解决方案易于移植,因此您需要使代码尽可能小,以使其适合我的软盘(我只希望携带一张软盘,但是如果您的源代码太大,我可以携带多个:P)。

输入值

  • 颜色列表(RGB,十六进制,名称等)
  • 文字格式

您的程序应该期望颜色列表至少包含2种颜色。
要格式化的文本可以是大于零的任何长度,并且字符将限于可打印的ascii。(提示:较长的文本输入可能要求您将中间颜色重新用于连续字符)

输出量

除了字体和/或标记/样式外,输出文本应与输入文本没有任何不同(注意:如果输出包含html标记,则需要对输入进行html编码)。您可以输出带有标记/样式的文本(html样式标签,控制台颜色等)或褪色文本的图片。除非输入中没有足够的字符来满足此要求,否则所有彩色十六进制应出现在输出中。如果是这种情况,请查阅优先级规则,以确定输出中应显示哪些颜色的十六进制。输出中的顺序或这些颜色仍应为输入顺序。

颜色优先规则

  1. 如果输入是一个字符,将使用第一种颜色
  2. 如果只有两个字符,将使用第一个和最后一个颜色
  3. 如果存在三种以上的颜色,并且多于一种字符,则应优先使用第一种和最后一种颜色,然后按输入顺序排列其余颜色。
  4. 如果字符多于颜色,则字符应使用中间颜色从一种颜色淡入另一种颜色

示例(优先级规则分别为1-3):
#颜色| 颜色0 | ... | 颜色n | 文字
3 ff0000 0000ff ffff00 M-> -> ->只有第一
3 ff0000 0000ff ffff00 hi忽略第二
4 ff0000 0000ff ffff00 0fff00 sup忽略第三

为了清楚起见,文本颜色应从一种颜色的十六进制逐渐淡入另一种颜色。淡变不必完全均匀,但是除非字符不足以使淡变好,否则淡变不应是突然的。通常,此淡入淡出是通过为rgb值增加/减少一定间隔来为每个字符选择中间颜色来实现的,该间隔由必须使用的字符数和颜色之间的差异决定。例如,如果我们需要介于红色(#ff0000)和黑色(#000000)之间的单一颜色,则可以选择#800000它位于中间的颜色。最佳输出将看起来很漂亮。

示例(优先级规则4):
3 ff0000 ff7f00 f0ff00 To be or not to be, that is the question...-> -OR-是否存在

<span style="color:#ff0000;">T</span><span style="color:#ff0600;">o</span><span style="color:#ff0c00;"> </span><span style="color:#ff1200;">b</span><span style="color:#ff1800;">e</span><span style="color:#ff1e00;"> </span><span style="color:#ff2400;">o</span><span style="color:#ff2a00;">r</span><span style="color:#ff3000;"> </span><span style="color:#ff3600;">n</span><span style="color:#ff3c00;">o</span><span style="color:#ff4300;">t</span><span style="color:#ff4900;"> </span><span style="color:#ff4f00;">t</span><span style="color:#ff5500;">o</span><span style="color:#ff5b00;"> </span><span style="color:#ff6100;">b</span><span style="color:#ff6700;">e</span><span style="color:#ff6d00;">,</span><span style="color:#ff7300;"> </span><span style="color:#ff7900;">t</span><span style="color:#ff7f00;">h</span><span style="color:#fe8500;">a</span><span style="color:#fe8b00;">t</span><span style="color:#fd9100;"> </span><span style="color:#fc9700;">i</span><span style="color:#fb9d00;">s</span><span style="color:#fba400;"> </span><span style="color:#faaa00;">t</span><span style="color:#f9b000;">h</span><span style="color:#f9b600;">e</span><span style="color:#f8bc00;"> </span><span style="color:#f7c200;">q</span><span style="color:#f6c800;">u</span><span style="color:#f6ce00;">e</span><span style="color:#f5d400;">s</span><span style="color:#f4da00;">t</span><span style="color:#f4e100;">i</span><span style="color:#f3e700;">o</span><span style="color:#f2ed00;">n</span><span style="color:#f1f300;">.</span><span style="color:#f1f900;">.</span><span style="color:#f0ff00;">.</span> 

在您的答案中,请指定输出的显示方式(以html,在控制台中,以图像等形式)。

*所有黑色背景仅用于强调颜色,并非必需

计分

这是,因此最短的答案(以字节为单位)获胜。
如果您设法为单个角色添加淡入淡出,那么我会永远认为您很酷(但是没有得分的奖励,因为这对某些语言不公平)



@LeakyNun在RGB空间中就足够了,另一个选项是#7f0000。
尼尔

并不是的。尝试以这种方式平均红色和绿色,看看是否变黄。要正确执行此方法,请采用均方根值,而不要采用直接平均法。
Leaky Nun

平均000000ff0000应为b40000255*sqrt((0+1)/2)
漏尼姑

1
@LeakyNun我从未说过需要平均颜色。我描述了一种简单的淡入淡出方法,这种方法有时可用于此类挑战。在这种情况下,我们将两种颜色的红色取平均值(并四舍五入)。我在挑战中还指出,渐变不必完全均匀。由您决定如何实现此淡入淡出。

Answers:


3

JavaScript(ES6),290字节

h=a=>f(a,a.shift());f=
(a,w)=>[...w].map((c,i)=>{l=w.length-1;m=a.length-1;s=o.appendChild(document.createElement('span'));s.textContent=c;s.style.color=`#${i?i-l?a[r=l%m,n=l/m|0,i<r*n+r?++n:i-=r,k=i/n|0,k++].replace(/./g,(c,d)=>((parseInt(c,16)*(n-i%n)+i%n*parseInt(a[k][d],16))/n|0).toString(16)):a[m]:a[0]}`;})
<textarea rows=10 cols=40 oninput="o.textContent='';h(this.value.split`\n`)">Type the text here and the colours on subsequent lines.
FF0000
00FF00
0000FF</textarea><div id=o>


1
看到它与代码片段一起实时发生确实很棒。:D
AdmBorkBork's

1

Pyth,126个字节

强制均方根平均值而不是直接算术平均值。

L%"<span style=\"color:#%s\">%s</span>",smt.H+256s*255@d2ebhbMm+hG*-eGhGcdHH=Q^RL2Q+smsyMC,hdCgRlhdCedC,ctlQPzC,QtQy,ez?tlzeQh

在线尝试!

样本输出:

样品


1

Java中,702 662个字符

打高尔夫球的两个功能:

import java.awt.*;String f(Color C,char c){return"<span style=\"color:#"+Integer.toHexString(C.getRGB()).substring(2)+";\">"+c+"</span>";}String c(String t,String[]h){String r="";int l=h.length,e=t.length(),i=0,s=0,g=1,c=-1,p,q,u;double d,m=0,v;char[]T=t.toCharArray();Color C[]=new Color[l],H[],a,b;for(;i<l;)C[i]=Color.decode(h[i++]);if(l>e){H=java.util.Arrays.copyOfRange(C,0,e);H[e-1]=C[l-1];H[0]=C[0];C=H;l=e;}d=--e/(l-1.);for(;++c<e;){a=C[s];b=C[g];p=b.getRed()-a.getRed();q=b.getGreen()-a.getGreen();u=b.getBlue()-a.getBlue();v=m/d;r+=f(new Color(a.getRGB()+((int)(v*p)<<16|(int)(v*q)<<8|(int)(v*u))),T[c]);if(++m>d){m-=d;s=g++;}}return r+f(C[l-1],T[e]);}

没人能读懂:这是类中没有版本的两个函数:

import java.awt.*;

public class Q80554 {

    static String format(Color color, char character) {
        return "<span style=\"color:#" + Integer.toHexString(color.getRGB()).substring(2) + ";\">" + character + "</span>";
    }

    static String colorizeB(String text, String[] hexColors) {
        String result = "";
        int colorsLength = hexColors.length, textLength = text.length(), i, currentStartColorPos = 0, currentGoalColorPos = 1, currentCharPos = -1, diffColorRed, diffColorGreen, diffColorBlue;
        double difference, goneDifference = 0, relativeChange;
        char[] textArray = text.toCharArray();
        Color colors[] = new Color[colorsLength], changer[], currentStartColor, currentGoalColor;

        for (i = 0; i < colorsLength;)
            colors[i] = Color.decode(hexColors[i++]);

        if (colorsLength > textLength) {
            changer = Arrays.copyOfRange(colors, 0, textLength);
            changer[textLength - 1] = colors[colorsLength - 1];
            changer[0] = colors[0];

            colors = changer;
            colorsLength = textLength;
        }

        // fade
        difference = --textLength / (colorsLength - 1.); // space between colors    

        for (; ++currentCharPos < textLength;) {
            currentStartColor = colors[currentStartColorPos];
            currentGoalColor = colors[currentGoalColorPos];

            diffColorRed = currentGoalColor.getRed() - currentStartColor.getRed();
            diffColorGreen = currentGoalColor.getGreen() - currentStartColor.getGreen();
            diffColorBlue = currentGoalColor.getBlue() - currentStartColor.getBlue();

            relativeChange = goneDifference / difference;

            result += format(new Color(currentStartColor.getRGB() + ((int) (relativeChange * diffColorRed) << 16 | (int) (relativeChange * diffColorGreen) << 8 | (int) (relativeChange * diffColorBlue))), textArray[currentCharPos]);

            if (++goneDifference > difference) {
                goneDifference -= difference;
                currentStartColorPos = currentGoalColorPos++;                   
            }
        }

        // last character always has last color
        return result + format(colors[colorsLength - 1], textArray[textLength]);
    }
}

在这里,您有自己的代码的上限。用法是通过调用colorize(或在高尔夫球版本中为c)并传递文本和十六进制颜色代码数组。该函数将返回一个带有HTML标签的String,就像OP所做的那样,因此您需要某种方式来呈现HTML。

随着问题的出现,该算法更加容易。第一个字符始终获得第一个颜色,最后一个始终获得最后一个颜色。如果文本中的颜色比字符多,我们只需遍历文本和颜色并应用它们。有趣的部分是带有淡入淡出的部分:首先,我要确定文本上颜色的距离。我基本上会计算两种给定颜色之间的红色,绿色和蓝色差异,然后将这种差异的一部分添加到第一种颜色,具体取决于字符在两种颜色之间的位置。如果它离开了两种颜色的间隔,我们将从新的颜色开始,再选择接下来的两种颜色。除了最后一个字符(我们知道总是最后一个颜色)以外的所有字符都重复此操作。这会产生非常美丽的褪色。

这个问题非常有趣!谢谢!

更新

现在,我不专门处理所有情况。相反,如果有两种颜色,我会修剪颜色,并对每个字符串应用淡入淡出功能。如果颜色多于文本,则将修剪颜色,并且淡入淡出功能将完全像简单的映射一样工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.