Convert 16 colors in CSS into RGB format

Source: Internet
Author: User

Annotations to the Dojo/_base/color module, source address Https://github.com/robinxiong/dojo/blob/master/_base/Color.js


function Fromhex (color) {/* Summary: Converts the CSS property value to the RGB-formatted alignment "#fff", 0xfff length For 4, a character occupancy represents an attribute, 0x10 = = 16 (2 of 4, 1 4 0) = = = 1 0000, then an F is 1111 placeholder for mask = 1111 for the post four bit (& to retain the four-bit value), others High clear 0 each traversal starts from the last four bits, so first get to B value, difficult to move 4 bit after getting to G, moving 4 bit, get to R get B property: 0000 1111 1111 1111 & 0000 0000 0000 1111 (only 16 bits are listed here, normal 32-bit) 0000 0000 0000 1111 B saved to variable c 0000 1111 1111 1111 >> 4, 0000 000          0 1111 1111 get g property 0000 0000 1111 1111 & 0000 0000 0000 1111 0000 0000 0000 1111 Get to G value, save to variable C         In the move four-bit, get R the value "FFFFFF", 0xffffff length is 8, every two characters represents a function 0xFF = 1111 1111 after each fetch 8 bits, just represents the color value */var t = {}, bits = (Color.length = = 4)? 4:8,//if it is shorthand, #fff, then bits is 4 bits, each one represents a property, the other 8 bits per two bits represent an attribute #ffffff00 mask = (1 << bits)-1; Represents a byte placeholder, moves left 4-bit or 8-bit, var a = (1 << 4)-1-10000-1, a.tostring(2); 1111, or 8-bit 1111 1111 color = number ("0x" + color.substr (1));        #ff0000 into 16-0xff0000;  if (IsNaN (color)) {return null;//color} ["B", "G", "R"].foreach (function (x) {var c =            Color & mask;            Color >>= bits; T[X] = bits = = 4? * C:C; 0xfff, an F should represent 255, which should be when [0-255], divided by 15 equal parts, each equal to 17 intervals.        So the obtained value needs to be multiplied by 17 to represent the value of 255 in RGB});        T.A = 1; Return t;//Color} console.log (Fromhex ("#00f"))//{r:0, g:0, b:255, a:1}


Convert 16 colors in CSS into RGB format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.