javascript解析器_JavaScript中的RGB颜色解析器

news/2024/7/19 15:02:55 标签: python, java, javascript, php, js
<a class=javascript解析器" width="403px" height="256px" style="outline: none;" />

javascript解析器

它是什么(What is it)

A JavaScript class that accepts a string and tries to figure out a valid color out of it. Some accepted inputs are for example:

一个JavaScript类,它接受字符串并尝试从中找出有效的颜色。 一些可接受的输入例如:

  • rgb(0, 23, 255)

    rgb(0,23,255)
  • #336699

    #336699
  • ffee66

    ffe
  • fb0

    fb0
  • red

  • darkblue

    深蓝
  • cadet blue

    学员蓝色

For more accepted inputs - see the demo.

有关更多可接受的输入,请参见演示。

Here's the javascript class - rgbcolor.js.

这是javascript类-rgbcolor.js

一些历史/动机 (Some history / motivation)

I was playing around with an idea (will post later) and I needed to get color information using the so-called computed styles. The thing is that I needed the exact amounts of Red, Green and Blue, so I needed to parse the value returned.

我当时正在研究一个想法(稍后再发布),我需要使用所谓的计算样式来获取颜色信息。 事实是,我需要确切数量的红色,绿色和蓝色,因此我需要解析返回的值。

In FireFox when you get a computed style, it's in the format rgb(xxx, yyy, zzz) In IE, it's #xxyyzz. So I needed to parse both formats.

在FireFox中,当您获得计算样式时,格式为rgb(xxx, yyy, zzz)在IE中,它是#xxyyzz 。 所以我需要解析两种格式。

I decided to take my script one step further and make the color parsing into a seperate class. Then I added those string values - red, green, etc.

我决定将脚本更进一步,将颜色解析为单独的类。 然后我添加了这些字符串值-红色,绿色等。

The result is something you can use, among other purposes, as a friendlier user input field.

结果是您可以将其用作更友好的用户输入字段的用途。

如何使用 (How to use)

The class is defied in a function RGBColor(). When you instantiate the class, you pass the string to be poarsed. The class has variables for the three channels - red, green and blue and methods to get the parsed value - toHex() and toRGB().

该类在函数RGBColor() 。 当您实例化该类时,您将传递字符串以进行初始化。 该类具有用于三个通道(红色,绿色和蓝色)的变量以及用于获取解析值的方法-toHex()和toRGB()。

Example use:

使用示例:

var color = new RGBColor('darkblue');
if (color.ok) { // 'ok' is true when the parsing was a success
    // alert channels
    alert(color.r + ', ' + color.g + ', ' + color.b);
    // alert HEX and RGB
    alert(color.toHex());
    alert(color.toRGB());
}

怎么运行的 (How it works)

  • The class accepts a string

    该类接受一个字符串
  • Any leading # is stripped; spaces are stripped

    除去任何前导#号; 空格被剥夺
  • There' s a check against a list of valid color names, such as "red" and "darkorange" and these are mapped to a Hex code

    会检查有效颜色名称的列表,例如“ red”和“ darkorange”,并将它们映射到十六进制代码
  • An array of objects is defined that have one regexp property and a function that knows what to do if the regexp find a match

    定义了一个对象数组,该对象具有一个regexp属性和一个知道regexp找到匹配项怎么办的函数
  • There is a quick validation that the channel values are between 0 and 255

    快速验证通道值在0到255之间
  • The two getters are defined - toHex() and toRGB()

    定义了两个吸气剂toHex()toRGB()

  • Finally there is a function that acts as both a self-documentation and self-uinttest, where a bunch of accepted values are automatically run through the class and parsed and the result is displayed as a help text.

    最后,有一个函数既可以用作自我说明文件,又可以用作自我测试,该函数会自动在类中运行并接受一堆接受的值并进行解析,并将结果显示为帮助文本。

The idea of the array of objects containing a regexp and a handler function is Simon Willison's. He did a script to parse dates, which I used and modified to work with time entries.

包含正则表达式和处理程序函数的对象数组的想法是Simon Willison的。 他做了一个脚本来分析日期,我使用并对其进行了修改以处理时间条目。

In my time parser I also introduced the idea of the self-documenting regexps and the help/test function which I'm reusing here again.

在我的时间解析器中,我还介绍了自我记录式正则表达式和帮助/测试功能的概念,在此我将再次使用它们。

全都是你的 (All yours)

Feel free to use the code for your own color picker tool or whatever you feel like. If you let me know how you use it, that would be even greater. Meanwhile any other comments are highly appreciated.

随意将代码用于自己的颜色选择器工具或任何您喜欢的颜色。 如果您让我知道如何使用它,那就更好了。 同时,任何其他评论都将受到高度赞赏。

And if I may rephrase a TV ad: Allowing your users to enter the color "Dark Khaki" - priceless! 😉

如果我可以改写电视广告:允许您的用户输入“ Dark Khaki”(深色卡其色)颜色-无价! 😉

Tell your friends about this post on Facebook and Twitter

在Facebook和Twitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/rgb-color-parser-in-javascript/

javascript解析器


http://www.niftyadmin.cn/n/1306884.html

相关文章

it 部门的建议_IT文化变革的5个误区和现实

为了推动数字化转型&#xff0c;IT组织必须拥抱变革。无效或过时的观念则会阻碍成功。Asanka Jayasuriya于2020年初开始加入SailPoint Technologies并担任工程高级副总裁兼首席技术官&#xff0c;部分原因在于该公司致力于一种注重创新、诚信、影响力和个人的文化。Jayasuriya作…

android cmd命令左键_Android系统Debug神器之dumpsys

1.概述 dumpsys是android系统里面的一个可执行文件。从名字来看&#xff0c;其主要作用是将当前android系统的一些信息dump出来&#xff0c;如activity、package等。dumpsys是一个分析android设备问题、查看运行状态、使用情况等十分有效的工具。查看所有支持的dump选项&#x…

webgl坐标转换_canvas与webgl坐标转换

canvas svg webgl threejs d3js 的区别canvas 绘制2D位图. Echarts是基于Canvas技术的可视化工具,底层封装了原生的JavaScript的绘图 API. canvas里面绘制的图形不能被引擎抓取,canvas中我们绘制图 ...CSS3、SVG、Canvas、WebGL动画精选整理一.CSS3动画 名称 用途 链接 阴影波纹…

菲波齐纳数列用python_python实现斐波那契数列 用递归实现求第N个菲波那切数列-Go语言中文社区...

斐波那契数列即著名的兔子数列&#xff1a;1、1、2、3、5、8、13、21、34、……数列特点&#xff1a;该数列从第三项开始&#xff0c;每个数的值为其前两个数之和&#xff0c;用python实现起来很简单&#xff1a;a0b1while b < 1000:print(b)a, b b, ab输出结果&#xff1a…

Boost库学习笔记-------内存管理

1.scoped_ptr 1.1 类似于标准库中的auto_ptr,区别是一旦获取指针所有权&#xff0c;就不在交出去。 实现机制&#xff1a;赋值运算符与赋值运算都声明为private auto_ptr<int> autoPtr1(new int(10)); auto_ptr<int> autoPtr2; autoPtr2autoPtr1; / /指针所有权发…

multisim中轻触开关在哪_轻触开关的使用细节和应用范围是什么?

点击上方蓝字关注「安装工程造价学习」每天推送轻触开关有接触电阻荷小、操作力误差小、规格多样化等方面的优势&#xff0c;因此轻触开关在电子设备及白色家电等方面得到广泛的应用如&#xff1a;影音产品、数码产品、通讯产品、家用电器、安防产品、玩具、电脑产品、健身器材…

Boost库学习笔记-----------实用工具之禁止复制 noncopyable

1.定义类时&#xff0c;如果我们不显示生命复制构造函数和赋值操作符编译器会默认为我们生成&#xff0c;如果想禁止复制&#xff0c;只需要私有 化复制构造函数和赋值操作符。 2.使用Boost库中的noncopyable能很好的减少我们的工作&#xff0c;如果每个类不想被复制&#…

添加编译宏_发布Qml宏预处理工具

​为Qml提供部分简单的预处理命令。0x00 初衷在不改变和影响原有语法的前提下提高qml开发效率。有一些qml项目兼顾与Qt4与Qt5版本&#xff0c;由于QML不用编译&#xff0c;但解释在运行时&#xff0c;使预处理指令不能用于直接通过QML。一般采用的方法是直接改文件内容&#xf…