<分区> 分区>
引用Why does HTML think “chucknorris” is a color?
下列分析是否正确?
首先,所有非十六进制字符都替换为“0”。
测试 -> 0e00000
如果它不能被 3 整除,则在其后附加“0”。
0e00000 -> 0e0000000
然后分成 3 个相等的组。
0e0000000 -> 0e0 000 000
然后获取每组的前 2 个字符并将它们连接在一起以获得您的颜色代码。
0e0 000 000 -> 0e0000
#0e0000 接近黑色。
但是如果你使用这个网站并输入字体颜色为“testing”,它会显示为红色阴影:http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_font_color
有什么我想念的吗?
附加在答案之后:
我正在编写一个 Android 应用程序,它需要我将 font color = ""解析为颜色代码。我把我拼凑的算法放在这里以供将来引用:
public String getColourCode(String nonStandardColour) {
String rtnVal = "#000000";
// first replace all non-hex characters
String converted = nonStandardColour.toLowerCase().replaceAll("[g-z]", "0");
System.out.println(nonStandardColour + " is now " + converted);
System.out.println("Length: " + converted.length());
if (converted.length() <= 3) {
// append "0"s if length != 3
while (converted.length() !=3) {
converted = converted + "0";
}
System.out.println("Converted colour is now " + converted);
// Length is 3, so split into 3 characters and prepend 0 to each
String[] colourArray = new String[3];
colourArray[0] = "0" + convertedOpNickColour.substring(0, 1);
colourArray[1] = "0" + convertedOpNickColour.substring(1, 2);
colourArray[2] = "0" + convertedOpNickColour.substring(2, 3);
rtnVal = "#" + Integer.toHexString(Color.rgb(
Integer.parseInt(colourArray[0], 16),
Integer.parseInt(colourArray[1], 16),
Integer.parseInt(colourArray[2], 16)));
}
else { // converted.length() is >= 4
System.out.println("Appending 0s until divisible by 3");
while(converted.length() % 3 != 0) {
converted = converted + "0";
}
System.out.println("Converted colour is now " + converted);
// divide into 3 equal groups
List<String> colourArray2 = new ArrayList<String>();
int index = 0;
while (index<converted.length()) {
colourArray2.add(converted.substring(
index, Math.min(index(converted.length()/3),converted.length())));
index+=(converted.length()/3);
}
System.out.printf("The 3 groups are:");
System.out.printf(colourArray2.get(0));
System.out.printf(colourArray2.get(1));
System.out.printf(colourArray2.get(2));
// if the groups are e.g. 0f0 0f0 0f0
if (rgbColour.get(0).length() >=3 ) {
rtnVal = Integer.toHexString(Color.rgb(
Integer.parseInt(colourArray2.get(0).substring(0,2), 16),
Integer.parseInt(colourArray2.get(1).substring(0,2), 16),
Integer.parseInt(colourArray2.get(2).substring(0,2), 16)));
// remove alpha
System.out.println("rtnVal is #" + rtnVal.substring(2));
return "#" + rtnVal.substring(2);
}
// groups are e.g. 0f 0f 0f
else {
rtnVal = Integer.toHexString(Color.rgb(
Integer.parseInt(colourArray2.get(0), 16),
Integer.parseInt(colourArray2.get(1), 16),
Integer.parseInt(colourArray2.get(2), 16)));
System.out.println("rtnVal is #" + rtnVal.substring(2));
return "#" + rtnVal.substring(2);
}
}
return rtnVal;
}
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚