473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

this.style.backgroundColor returns rgb but...

halu there!! i'm a 1month experienced java scripter but got a good hang of it already. hehehe.. i developed a "color selection" tool, similar to the one in flash when you choose a color for a graphic, with CSS only. the product is finished. whenever i select a color, its color will be displayed in a box whose id name is contained in variable "color_box" and the equivalent color value will appear in a text box with id contained in the variable "color_text" but only in rgb format.

it's here, check it out: http://glitzy3.com/layouts.php (FIREFOX ONLY)

this.style.backgroundColor returns an RGB value like rgb(255,255,255) but i want it to return a hex value like #FFFFFF. is that possible without having to go through a "RGB2HEX" function? i can't use a function no more because my code seems to be too complex that it won't "fit" or work anymore if i include a function which returns the hex.

this is a part of the script within a function, which receives two values,
Expand|Select|Wrap|Line Numbers
  1. function(color_box, color_text):
  2. ....
  3. var color="this.style.backgroundColor";
  4. ....
  5. color_table = color_table.concat("<td onclick=\"show_color_table(false)\" onmouseover=\"this.style.cursor=\'help\',document.getElementById(\'",color_text,"\').value=",color,",document.getElementById(\'",color_box,"\').style.backgroundColor=",color,"\" style=\"background-color:rgb(");
  6.         color_table = color_table.concat(R,",",G,",",B,");\"></td>");
....
the bold portion is the problem. i cant use RGBToHEX(color) because the value of color sent to the function RGBToHex will not anymore be the color value but the text "this.style.backgroundColor". so it seems that the variable is far away from getting the actual this.style.backgroundColor if i put a function.

check out my code its in a php document at:
http://glitzy3.com/layouts.php (view with FireFox NOT IE)

AH SINCE I DIDN'T REALIZE THAT MY CODE DOENST WORK IN INTERNET EXPLORER, COULD YOU PLEASE GIVE ME THE "SOURCE CODE" IN MY CODE THAT CAUSED THE MISERY?
Sep 24 '07 #1
6 10682
pbmods
5,821 Expert 4TB
Heya, Fallen Angel. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]
Sep 25 '07 #2
pbmods
5,821 Expert 4TB
Did you put a '#' in front of the hex code?

Expand|Select|Wrap|Line Numbers
  1. document.getElementById(' ... ').style.backgroundColor = '#' + hexCode;
  2.  
Sep 25 '07 #3
Did you put a '#' in front of the hex code?

Expand|Select|Wrap|Line Numbers
  1. document.getElementById(' ... ').style.backgroundColor = '#' + hexCode;
  2.  
oops, sorry for not putting the &lt;code=javascript&gt; i didnt know hehe..

yeah i used # in front of the hex for the first three table color columns and for the rest i had to use rgb(xxx,yyy,zzz), but EITHER WAY i use, this.style.backgroundColor, will always return rgb(xxx,yyy,zzz) in firefox and #XXYYZZ in IE as i researched. can FF not return #XXYYZZ?

in the bold portion of my code in color, i tried passing color in function like this: RGBToHex(color)

and if i use function as:
Expand|Select|Wrap|Line Numbers
  1. function RGBToHEX(str){
  2.     return str;
  3. }
  4.  
it just works fine in that it just returns the passed value. BUT if i try to put methods on function variable "str" like:
str = str.slice(4) (which is supposed to return xxx,yyy,zzz) with rgb( stripped off,
then it will never return anything. i tried any string methods, it won't return anything.

Maybe it has something to do like when i receive a color rgb(xxx,yyy,zzz), it cannot be edited as string. do i have to convert it to a string or something to be able to apply string methods to it?
Sep 26 '07 #4
ah nevermind.. it's ok, i solved it, i included the RGBToHex into the string instead of concatenating it. this is the way i did it (bold part):
Expand|Select|Wrap|Line Numbers
  1. color_table = color_table.concat("<td onclick=\"show_color_table(false)\" onmouseover=\"this.style.cursor=\'help\',document.getElementById(\'",color_text,"\').value=RGBToHEX(",color,"),document.getElementById(\'",color_box,"\').style.backgroundColor=",color,"\" style=\"background-color:rgb(");
  2.         color_table = color_table.concat(R,",",G,",",B,");\"></td>");
  3.  
the problem now is that my product does not work in IE. does IE even support javascript. or what is wrong with my code that makes it not work in IE?
Sep 26 '07 #5
dmjpro
2,476 2GB
Have a look at this.

Expand|Select|Wrap|Line Numbers
  1. function RGBtoHEX(str)
  2. {
  3.  var arr = str.split(",");
  4.  var r = arr[0].substring(4),
  5.  g = arr[1],
  6.  b = arr[2].substring(0,arr[2].length-1);
  7.  return "#"+r+g+b;
  8. }
  9.  
Good Luck!

Kind regards,
Dmjpro.
Sep 26 '07 #6
wow!! i didn't know that the split function was that powerful, in fact its the first time i used it. hehehe...

i just added an additional function that converts your #255255255 to #FFFFFF
with the DECToHex function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. return "#" + DECToHEX(r) + DECToHEX(g) + DECToHEX(b)
  3.  
  4. function DECToHEX(N) {
  5.     hex_set = "0123456789ABCDEF"
  6.      return hex_set.charAt((N-N%16)/16) + hex_set.charAt(N%16);
  7. }
  8.  
thanx alot DMJPRO for that very efficient code!
Sep 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: joealey2003 | last post by:
Hi all... I included a css file on my html and i need to check some properties. The html is: <style id="myid" src="mycsspage.css"> </style> Now i need something to access it like: ...
3
by: Jane D | last post by:
I have got a bookmarklet for use with Opera which highlights all occurrences of some text in the displayed page. I find it very useful. Sometimes I need to use two or three different colours...
2
by: Martin | last post by:
I have some functions in a script in which I'm manipulating the innerText and background colors of certain rows in a table. The lines below work OK in IE but when I try them in Mozilla, I get an...
5
by: J.Marsch | last post by:
All: I have an interesting problem in front of me. does this sound reasonable, or ridiculous? I have to build something that is sort of like a style sheet for Windows controls. Picture a...
3
by: SpamProof | last post by:
I got an animated gif that is a barber pole spinning that I want to use as a progress bar. The problem is that is stops spinning (shows 1 frame) when my browser is processing a submited request...
7
by: Jim Carlock | last post by:
Does a SELECT element (listbox) need to be inside a FORM element? The code I'm playing with: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
2
by: Alex | last post by:
Trying to change a few tags by swapping color values to my own. It doesn't give any error, yet it doesn't perform. Is there an error in my logic somewhere? var my_i =...
3
by: pld888 | last post by:
https://turbotaxweb.turbotaxonline.intuit.com/open/registration/SignIn.htm anyone know which 3rd party textbox this is???
9
by: Piotr K | last post by:
Ok, I tried simply everything that came to my mind and now I ran out of ideas, but to the point - take a look at the code below // GetStyle returns given style value (works fine)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.