473,750 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

this.style.back groundColor returns rgb but...

7 New Member
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.back groundColor 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.bac kgroundColor". so it seems that the variable is far away from getting the actual this.style.back groundColor 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 10718
pbmods
5,821 Recognized Expert Expert
Heya, Fallen Angel. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascrip t]
JavaScript code goes here.
[/CODE]
Sep 25 '07 #2
pbmods
5,821 Recognized Expert Expert
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
fallen angel
7 New Member
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=javasc ript&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.back groundColor, 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
fallen angel
7 New Member
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 Top Contributor
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
fallen angel
7 New Member
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
2119
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: alert(document.getElementById("myid").bottomline.color);
3
1765
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 for different keywords I want highlighting on the same page. I have done this by changing the original word 'yellow' for other javascript colour words. I know next to nothig about javascript. So I would ask if anyone
2
2535
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 error that says: "document.getElementById('TableX').rows is not a function". thisRow = document.getElementById('TableX').rows(1); thisRow.style.background = 'white'; Can anyone give me a clue as to how to fix this so that it will work
5
1289
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 collection of dissimilar value types: a couple of Color types, some ints, some strings etc.
3
2863
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 that might take 5-8 seconds. I tried to putting the gif in a sepearate page and referencing it using iframe thinking that if the gif is on another page & calling it from my main page, it would not be affected by my submittals and continue to...
7
6896
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
1264
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 = document.getElementsByTagName( 'td' ); for (i = 0; i < my_i.length; i++) { if ((my_i.hasAttribute( 'BGCOLOR' )) && (my_i.getAttributeNode( 'BGCOLOR' ).value == '#F7FFFB' )) my_i.setAttribute( 'BGCOLOR', '#00FF00' );
3
1575
by: pld888 | last post by:
https://turbotaxweb.turbotaxonline.intuit.com/open/registration/SignIn.htm anyone know which 3rd party textbox this is???
9
1273
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) document.getElementById("inner").style.backgroundColor = GetStyle("box", "backgroundColor"); document.getElementById("box").style.backgroundColor = "transparent"; <div id="box" style="width: 100px; height: 100px; background-color: Red">
0
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.