Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert rgb value in hexadecimal system

    function convertRgb(rgb) {
  // This will choose the correct separator, if there is a "," in your value it will use a comma, otherwise, a separator will not be used.
  var separator = rgb.indexOf(",") > -1 ? "," : " ";


  // This will convert "rgb(r,g,b)" into [r,g,b] so we can use the "+" to convert them back to numbers before using toString 
  rgb = rgb.substr(4).split(")")[0].split(separator);

  // Here we will convert the decimal values to hexadecimal using toString(16)
  var r = (+rgb[0]).toString(16),
    g = (+rgb[1]).toString(16),
    b = (+rgb[2]).toString(16);

  if (r.length == 1)
    r = "0" + r;
  if (g.length == 1)
    g = "0" + g;
  if (b.length == 1)
    b = "0" + b;

  // The return value is a concatenation of "#" plus the rgb values which will give you your hex
  return "#" + r + g + b;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: getcontext canvas not autocomplete 
Javascript :: Install React Navigation dependencies into main project folder 
Javascript :: swiperjs doesnot works inside modal 
Javascript :: momen js get time zone code from location name 
Javascript :: ck editr value submit jquery ajax 
Javascript :: javascript if null use other value 
Javascript :: how to press enter key automatically using javascript 
Javascript :: tips and tricks for javascript 
Javascript :: count all items inside 2nd ul using jquery 
Javascript :: how to edit local json files using node 
Javascript :: input file selector on button click vuejs 
Javascript :: react native typescript nodejs timeout 
Javascript :: reorder them so that more specific routes come before less specific routes 
Javascript :: add component to route 
Javascript :: 4.7.2. Compound Assignment Operators¶ 
Javascript :: bjsmasth update 
Javascript :: exchange array.include(string) in Javascript to array.indexOf(string) == -1 in Typescript 
Javascript :: react app link description preview 
Javascript :: { "name":"walk dog", "isComplete":true } 
Javascript :: delete all cookies javascript 
Javascript :: cproblem upgrading node on windws 
Javascript :: Spotify analytics intergration 
Javascript :: javascript formdata include object 
Javascript :: what to say to your ex 
Javascript :: how to require token in discord.js without client 
Javascript :: where does tls come in the osi layer 
Javascript :: fat arrow return object 
Javascript :: routing in react jps 
Javascript :: how to remove tashkeel from arabic charactor 
Javascript :: replace function javascript recurrent 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =