Results 1 to 6 of 6

Thread: How to do Color Conversion in Javascript?

  1. #1
    Join Date
    Aug 2006
    Posts
    148

    How to do Color Conversion in Javascript?

    Hi friends,
    I want your help for converting the colors between different color systems. I have tried a lot of scripts but I was not getting the expected output. Can anyone explain me how to do Color Conversion in Javascript? Also if possible tell how to convert RGB Color to HTML Hex.?? Extremely Thanks for your Support in Advance.!!
    The Gaming Machine:
    Processor: Core 2 Duo E6400
    Motherboard: Asus P5W DH Deluxe
    RAM: 2GB XMS2 Corsair PC2-6400
    Video: ATI Radeon X1900XT 512MB w/ Arctic Accellero
    Drive: 74GB "WD Raptor" 10000RPM
    Sound: Creative SoundBlaster X-Fi
    Watercooling: *Soon* Zalman Reserator

  2. #2
    Join Date
    Mar 2008
    Posts
    672

    Re: How to do Color Conversion in Javascript?

    For doing the color conversion, first you will need to create color object you want to convert from. There are three types of color encoding system that the JavaScript supports :
    • RGB
    • HSV
    • CMYK

    You can create color object as following :
    Code:
    var obj = new RGB(50 , 60 , 70 );
    var obj = new HSV(50 , 60 , 70 );
    var obj = new CMYK(50 , 60 , 70 , 80 );

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to do Color Conversion in Javascript?

    After creating the color object, you will need to pass that object to appropriate “ColorConverter” method. Like I have provided you with the following coding :
    1. To convert from HSV to RGB use library like :
      Code:
      var result = ColorConverter.toRGB(new HSV(50, 60, 70));
      alert("RGB:" + result.r + ":" + result.g + ":" + result.b);
    2. To convert from RGB to HSV use library like :

    Code:
    var result = ColorConverter.toHSV(new RGB(50, 60, 70));
    alert("HSV:" + result.h + ":" + result.s + ":" + result.v);

  4. #4
    Join Date
    Jul 2006
    Posts
    289

    Re: How to Convert RGB Color to HTML Hex?

    You also want some information about the converting RGB Color to HTML Hex as you mentioned in your query.! Before doing the coding for the same, I think that you should clear the concepts regarding the same. The RGB (Red, Green, Blue) values of color that are converted into Hex, should be known to the HTML. The 16 different values are stored in 1 character by the Hex method. You might have observed that you have to take a color code that was generated in one program while using the color on Web pages.
    Signatures reduce available bandwidth

  5. #5
    Join Date
    Mar 2008
    Posts
    349

    Re: How to do Color Conversion in Javascript?

    I think that you might have get little confused after seeing the information about the conversion of colors in JavaScript. So I thought to provide you the sample of the code, so that you can understand it in more better way. Here is the sample of the JavaScript which demonstrates how to convert RGB values to hex string :
    Code:
    function RGB2HTML(red, green, blue)
    {
        var decColor = red + 256 * green + 512 * blue;
        return decColor.toString(16);
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: How to do Color Conversion in Javascript?

    You can also use the following code for converting the colors into Hex :

    Code:
    function RGBToHex(rgb) 
    {
    var char = "12345QWERT";
    return String(char.charAt(Math.floor(rgb / 16))) + String(char.charAt(rgb - (Math.floor(rgb / 16) * 16)));
    }

Similar Threads

  1. Replies: 3
    Last Post: 18-03-2012, 07:33 PM
  2. Color Alignment in HP Color LaserJet CM1312
    By Hameeda-K in forum Hardware Peripherals
    Replies: 4
    Last Post: 26-11-2011, 04:45 PM
  3. Make Active windows border color and taskbar icon color
    By pokemon5 in forum Customize Desktop
    Replies: 5
    Last Post: 04-12-2010, 11:27 PM
  4. Conversion of CD Rom to Dvd Rom- Can it be possible?
    By Kusagra in forum Hardware Peripherals
    Replies: 3
    Last Post: 16-11-2009, 12:22 PM
  5. XP Color theme with dark window background color?
    By sayeed in forum Windows XP Support
    Replies: 4
    Last Post: 27-06-2008, 01:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,246,763.99149 seconds with 17 queries