Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:53:39+00:00 2026-05-27T20:53:39+00:00

Possible Duplicate: Javascript color gradient I have color one (let’s say yellow) and color

  • 0

Possible Duplicate:
Javascript color gradient

I have color one (let’s say yellow) and color two (blue) – they make up a gradient.
Based on a value of 0 to 100, (0 being yellow and 100 being blue), I’d like to represent a mixture of color one and two.

I am trying to do this in a mobile browser (safari specifically).

Is there a way to do this in javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:53:40+00:00Added an answer on May 27, 2026 at 8:53 pm

    If what you’re trying to do is to create a color that is some percentage (0-100) between two other colors, you can do that with this javascript:

    function makeGradientColor(color1, color2, percent) {
        var newColor = {};
    
        function makeChannel(a, b) {
            return(a + Math.round((b-a)*(percent/100)));
        }
    
        function makeColorPiece(num) {
            num = Math.min(num, 255);   // not more than 255
            num = Math.max(num, 0);     // not less than 0
            var str = num.toString(16);
            if (str.length < 2) {
                str = "0" + str;
            }
            return(str);
        }
    
        newColor.r = makeChannel(color1.r, color2.r);
        newColor.g = makeChannel(color1.g, color2.g);
        newColor.b = makeChannel(color1.b, color2.b);
        newColor.cssColor = "#" + 
                            makeColorPiece(newColor.r) + 
                            makeColorPiece(newColor.g) + 
                            makeColorPiece(newColor.b);
        return(newColor);
    }
    

    This function assumes the gradient is made with linear interpolation between each r, g and b channel value of the two endpoint colors such that the 50% gradient value is the midpoint of each r,g,b value (halfway between the two colors presented). Once could make different types of gradients too (with different interpolation functions).

    To assign this result to a background, you use the CSS color value I’ve added to the return result like this:

    // sample usage
    var yellow = {r:255, g:255, b:0};
    var blue = {r:0, g:0, b:255};
    var newColor = makeGradientColor(yellow, blue, 79);
    element.style.backgroundColor = newColor.cssColor;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Random Color generator in Javascript I have some data I'd like to
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: Graph visualization code in javascript? I have to integrate a graph with
Possible Duplicate: Explain JavaScript's encapsulated anonymous function syntax I have just read a javascript
Possible Duplicate: In JavaScript can I make a “click” event fire programmatically for a
Possible Duplicate: In Javascript, why is the “this” operator inconsistent? I have the following
Possible Duplicate: Javascript function to convert color names to hex codes Is it possible
Possible Duplicate: JavaScript: formatting number with exactly two decimals Maybe I'm going about this
Possible Duplicate: Javascript closure inside loops - simple practical example How can I make
Possible Duplicate: What does the leading semicolon in JavaScript libraries do? I have noticed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.