473,569 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rgb function with variable parameters?

4 New Member
hello there

i have some problem with rgb. i am writing a color selector , 3 sliders demonstrate 3 color , red and green and blue , then the mix color appears in a div named 'result' in the end of page. consider i have 3 variables which indicates 3 magnitude for r , g , and b. and this is my code :
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("result").style.backgroundColor = 'rgb(r,g,b)'
but this is not working at all, and when i use below code ,instead of the above one , it is working :
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("result").style.backgroundColor = 'rgb(10,10,10)'
but be sure that r and g abd b variables are all correct. because i use 'alert' function and all of them store a number between 0 and 255. i wonder if javascript does not compile the code between ' and ' , i mean rgb function , and pass the function to another program like html to compile, and html has no clue on r and g and b variables. am i right? is there any way to help me?
Oct 17 '08 #1
7 8058
zaphod42
55 New Member
your variables can't be part of the string:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById("result").style.backgroundColor = 'rgb('+r+','+g+','+b+')'
since the backgroundColor is a string, you have to concatenate it using the function supplied by javascript ( + )
e.g.:

Expand|Select|Wrap|Line Numbers
  1. var a='ABC'
  2. var b='DEF'
  3. var c=a+b
  4. alert(c)
will alert 'ABCDEF'
and

Expand|Select|Wrap|Line Numbers
  1. var c='let us begin with '+a+' and continue with '+b
  2. alert(c)
will alert ''let us begin with ABC and continue with DEF
Oct 17 '08 #2
gits
5,390 Recognized Expert Moderator Expert
yes ... another option is to use a kind of util-method for this, since this is a common task and you would have a centralized handling that might be of advantage in larger projects ... here is an example:

Expand|Select|Wrap|Line Numbers
  1. // a method that should return the desired string
  2. function setRGB(r, g, b) {
  3.     var s = 'rgb(%s, %s, %s)';
  4.     return sprintf(s, r, g, b);
  5. }
  6.  
  7. // replaces all %s in a string with the passed params
  8. // in the passed order
  9. // such a method is often useful ;)
  10. function sprintf(s) {
  11.     for (var i = 1, l = arguments.length; i < l; i++) {
  12.         s = s.replace(/%s/, arguments[i]);
  13.     }
  14.  
  15.     return s;
  16. }
  17.  
  18. // then you might always use
  19. document.getElementById("result").style.backgroundColor = setRGB(r, g, b);
kind regards
Oct 17 '08 #3
zaphod42
55 New Member
iteration is something you could use, I would just build the function to:

Expand|Select|Wrap|Line Numbers
  1. setColor(myElement,'150,150,150')
  2.  
  3. function setColor(el,vals){
  4.     el.style.backgroundColor='rgb('+vals+')'
  5. }
  6. or
  7. setColor(myElement,r,g,b)
  8. function setColor(el,r,g,b){
  9.     el.style.backgroundColor='rgb(' +r+ ',' +g+ ',' +b+ ')'
  10. }
  11.  
or

Expand|Select|Wrap|Line Numbers
  1. myElement.style.backgroundColor=getColor('150,150,150')
  2.  
  3. function getColor(vals){
  4.     return ('rgb('+vals+')')
  5. }
  6. or
  7. myElement.style.backgroundColor=getColor(r,g,b)
  8.  
  9. function getColor(vals){
  10.     return (  'rgb(' + r + ',' + g + ',' + b + ')'  )
  11. }
  12.  
Oct 17 '08 #4
gits
5,390 Recognized Expert Moderator Expert
of course for only one case my example might be overhead but have a look at the sprintf() method ... that could be used universaly for replacing of things in any string s (regardless of the number of parts that should be affected in a string) ... that was why i called it a util method ...
Oct 17 '08 #5
zaphod42
55 New Member
excellent point, that is a very handy method, I've recently had to deal with very specific regExp functions, so I suppose I'm thinking on a much smaller scale:)
Oct 17 '08 #6
mzd12111
4 New Member
Thanks Men(women)
Your guides were led to Victory
Oct 20 '08 #7
gits
5,390 Recognized Expert Moderator Expert
no problem :) ... you're welcome ... post back to the forum anytime you have more questions ...

kind regards
Oct 20 '08 #8

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

Similar topics

9
3680
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can...
1
2104
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and automatically builds querystrings for my redirect URLS. The code for this follows. It defines an ASP Dictionary object, and key/value pairs for each,...
1
2588
by: John Miles | last post by:
Hi -- This is a bit of an implementation-specific problem, but I'd like to post it here to see if there's a general answer within the auspices of the language. I'm developing a high(er)-level scripting language designed explicitly to complement C++. One of the language's features is the ability to pass "out" parameters to an MSVC...
3
14916
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
8
2949
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the...
5
1912
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the funciton name is a variable. Example: dim variable as string variable = "thisfunction()" call variable <---this will not work
39
7616
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
64
3335
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be "refactor-ed" to be
18
4329
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters,...
28
4294
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7974
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...
0
5219
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...
0
3653
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...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
938
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...

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.