• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

JavaScript - Convert RGB to Hex

bright

Content Creator
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
42
Likes
24
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 900 XP
In this tutorial we will create a Convert RGB to Hex using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "navbar-brand"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Convert RGB to Hex</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-9"

    >
  18. <div

    class

    =

    "form-inline"

    >
  19. <h4

    >

    RGBA</

    h4

    >
  20. <label

    >

    R:</

    label

    >
  21. <input

    type

    =

    "number"

    id

    =

    "r"

    class

    =

    "form-control"

    style

    =

    "width:20%;"

    min=

    "0"

    max=

    "255"

    /

    >
  22. <label

    >

    G:</

    label

    >
  23. <input

    type

    =

    "number"

    id

    =

    "g"

    class

    =

    "form-control"

    style

    =

    "width:20%;"

    min=

    "0"

    max=

    "255"

    /

    >
  24. <label

    >

    B:</

    label

    >
  25. <input

    type

    =

    "number"

    id

    =

    "b"

    class

    =

    "form-control"

    style

    =

    "width:20%;"

    min=

    "0"

    max=

    "255"

    /

    >
  26. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "convert()"

    ><span

    class

    =

    "glyphicon glyphicon-arrow-right"

    ></

    span

    >

    Convert</

    button

    >
  27. </

    div

    >
  28. </

    div

    >
  29. <div

    class

    =

    "col-md-3"

    >
  30. <h4

    >

    HEX</

    h4

    >
  31. <input

    type

    =

    "text"

    id

    =

    "hex"

    class

    =

    "form-control"

    readonly

    =

    "readonly"

    /

    >
  32. </

    div

    >
  33. </

    div

    >
  34. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  35. </

    body

    >
  36. </

    html

    >

Creating the Script

This code contains the script of the application. This code will convert the rbg into hex for better use in the layout. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function

    convert(

    )

    {
  2. var

    r =

    document.getElementById

    (

    "r"

    )

    .value

    ;
  3. var

    g =

    document.getElementById

    (

    "g"

    )

    .value

    ;
  4. var

    b =

    document.getElementById

    (

    "b"

    )

    .value

    ;
  5. var

    hex =

    document.getElementById

    (

    "hex"

    )

    ;

  6. if

    (

    r ==

    ""

    ||

    g ==

    ""

    ||

    b ==

    ""

    )

    {
  7. alert(

    "Please enter something!"

    )

    ;
  8. }

    else

    {
  9. if

    (

    r >

    255

    ||

    g >

    255

    ||

    b >

    255

    )

    {
  10. alert(

    "The given value is too large enough to convert!"

    )

    ;
  11. }

    else

    {
  12. var

    rgb=

    "rgb("

    +

    r+

    ", "

    +

    g+

    ", "

    +

    b+

    ")"

    ;
  13. hex.value

    =

    rgb2hex(

    rgb)

    ;
  14. }
  15. }
  16. }



  17. function

    rgb2hex(

    rgb)

    {
  18. rgb =

    rgb.match

    (

    /^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i

    )

    ;
  19. return

    (

    rgb &&

    rgb.length

    ===

    4

    )

    ?

    "#"

    +
  20. (

    "0"

    +

    parseInt(

    rgb[

    1

    ]

    ,

    10

    )

    .toString

    (

    16

    )

    )

    .slice

    (

    -

    2

    )

    +
  21. (

    "0"

    +

    parseInt(

    rgb[

    2

    ]

    ,

    10

    )

    .toString

    (

    16

    )

    )

    .slice

    (

    -

    2

    )

    +
  22. (

    "0"

    +

    parseInt(

    rgb[

    3

    ]

    ,

    10

    )

    .toString

    (

    16

    )

    )

    .slice

    (

    -

    2

    )

    :

    ''

    ;
  23. }

⚙ Live Demo

There you have it we successfully created a Convert RGB to Hex using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,513

356,536

356,563

Top
Raidforums