Jump to content
  • 0

class color


nim
 Share

Question

Написал полезный класс, мне он показался довольно удобным, а его функция tohex мне кажется самой быстрой среди аналогов, вобщем я не удержался и решил поделится со всеми :o

 this.color = function(c) // c - цвет в формате "#000000"
{
if (c.charAt(0) == '#') c = c.substr(1);
var i = parseInt(c, 16);

this.r = i >> 16;
this.g = (i >> 8) & 255;
this.b = i & 255;

this.tohex = function()
{
return '#' + byte2hex(this.r) + byte2hex(this.g) + byte2hex(this.b);
}

function byte2hex(b)
{
var b = parseInt(b);
var a1 = b / 16;
var a2 = parseInt(a1);
var a3 = 0;
if (a1 != a2){ a3 = b - a2 * 16; }
var str = "";
if (a2 > 9) { str += String.fromCharCode(55 + a2); } else {str += a2;}
if (a3 > 9) { str += String.fromCharCode(55 + a3); } else {str += a3;}
return str;
}
}

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy