changing background colour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • david.graham18

    #1

    changing background colour

    Hi
    I spotted some nice code to change the background colour of a web page to
    one of four different colours at random but I can't find it now!

    The method was to select a number at random from 1 to 4 by using the rnd()
    function and then dividing by modulus 4. The result was then used to select
    a cell in a 4 cell array which was holding a different colour in each cell
    of the array. I would like to have this code but lack the knowledge to
    re-create it. Anyone able to do this please.

    thanks
    David


  • Michael Winter

    #2
    Re: changing background colour

    On Tue, 10 Aug 2004 17:06:01 GMT, david.graham18
    <david.graham18 @ntlworld.com> wrote:
    [color=blue]
    > The method was to select a number at random from 1 to 4 by usingthe
    > rnd() function and then dividing by modulus 4.[/color]

    Wouldn't it be more sensible to generate a value between zero and three?
    [color=blue]
    > The result was then used to select a cell in a 4 cell array whichwas
    > holding a different colour in each cell of the array. [...][/color]

    var changeBackgroun d = (function() {
    var colours = ['#ffffff', '#000000'];
    var b = null, s = null;

    function random(n) {
    return Math.floor(n * Math.random());
    }

    if((b = document.body) && (s = b.style) &&
    'string' == typeof s.backgroundCol or)
    {
    return function() {
    s.backgroundCol or = colours[random(colours. length)];
    };
    } else {
    return function() {};
    }
    })();

    This creates a function, changeBackgroun d(), that will randomly select a
    colour in the colours array and use it to change the background colour for
    the document body. You can add as many colours as you want to the array.
    Just make sure they're a valid CSS colour value[1], and they leave the
    page readable (though that goes without saying).

    If you want to change the background just once, you could simplify the
    above to:

    (function() {
    var colours = ['#ffffff', '#000000'];
    var b = null, s = null;

    function random(n) {
    return Math.floor(n * Math.random());
    }

    if((b = document.body) && (s = b.style) &&
    'string' == typeof s.backgroundCol or)
    {
    s.backgroundCol or = colours[random(colours. length)];
    }
    })();

    Hope that helps,
    Mike


    [1] See the CSS specification for valid values
    <URL:http://www.w3.org/TR/REC-CSS2/syndata.html#co lor-units>.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail

    Comment

    • david.graham18

      #3
      Re: changing background colour


      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
      news:opscizrndr x13kvk@atlantis ...[color=blue]
      > On Tue, 10 Aug 2004 17:06:01 GMT, david.graham18
      > <david.graham18 @ntlworld.com> wrote:
      >[color=green]
      > > The method was to select a number at random from 1 to 4 by usingthe
      > > rnd() function and then dividing by modulus 4.[/color]
      >
      > Wouldn't it be more sensible to generate a value between zero and three?
      >[/color]
      Hi
      Thanks for the reply, I will try to understand your code. On the numbers 1
      to 4 thing, dividing by modulus 4 will only produce results 0, 1, 2 or 3 - I
      think. I might have caused confusion by stating it produces numbers 1 to 4

      thanks
      David


      Comment

      • Michael Winter

        #4
        Re: changing background colour

        On Tue, 10 Aug 2004 18:48:27 GMT, david.graham18
        <david.graham18 @ntlworld.com> wrote:

        [snip]
        [color=blue]
        > Thanks for the reply, I will try to understand your code.[/color]

        If there's anything in particular you want to know, do ask.
        [color=blue]
        > On the numbers 1 to 4 thing, dividing by modulus 4 will onlyproduce
        > results 0, 1, 2 or 3 - I think. I might have causedconfusion by stating
        > it produces numbers 1 to 4[/color]

        Indeed it will. The modulus operator returns the remainder after a
        division, so:

        1 % 4 = 0 r 1
        2 % 4 = 0 r 2
        3 % 4 = 0 r 3
        4 % 4 = 1 r 0
        etc.

        However, it's much simpler (and more efficient) to produce 0..3 from the
        offset.

        Good luck,
        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail

        Comment

        • david.graham18

          #5
          Re: changing background colour


          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opscjkq7mf x13kvk@atlantis ...[color=blue]
          > On Tue, 10 Aug 2004 18:48:27 GMT, david.graham18
          > <david.graham18 @ntlworld.com> wrote:
          >
          > [snip]
          >[color=green]
          > > Thanks for the reply, I will try to understand your code.[/color]
          >[/color]
          Hi
          I need to learn more Javascript to be able to understand your code. I have
          only a very basic understanding at the moment. You would have to explain
          each line of your code for me to understand it - I don't expect you to go to
          that sort of trouble, but your help is most appreciated. I suppose I liked
          the simple version of a random colour picker as it was easy to get to grips
          with, I've no doubt that your version is far supperior but unfortunately it
          is too difficult for me.

          thanks
          David


          Comment

          Working...