1

画面を横切って移動するときに、緑、オレンジ、赤から変化するプログレスバータイプのものを作成したいと考えています。例えば:

ここに画像の説明を入力してください

特定のポイントでの色を見つけるにはどうすればよいですか?

4

3 に答える 3

5

編集:これを実装するjsfiddleへのリンクは次のとおりです:http://jsfiddle.net/EAM9a/

最も簡単な方法では、色の間で線形補間を行うことができます。物事を簡単にするために、進行状況が0.0から1.0になると仮定しましょう。それで

0.0 - green  - rgb(0,   100, 0)
0.5 - orange - rgb(255, 165, 0)
1.0 - red    - rgb(139,   0, 0)

次に、必要な色に応じて、緑とオレンジの間、またはオレンジと赤の間を補間することができます。

var  green = [0, 100, 0],
    orange = [255, 165, 0],
       red = [139, 0, 0];

function color(val) {
  if (val < 0.5) {
    return colorToString(interpolate(val * 2, green, orange));
  } else {
    return colorToString(interpolate((val-0.5) * 2, orange, red));
  }
}

// val should be in the range [0.0, 1.0]
// rgb1 and rgb2 should be an array of 3 values each in the range [0, 255]
function interpolate(val, rgb1, rgb2) {
  var rgb = [0,0,0];
  var i;
  for (i = 0; i < 3; i++) {
    rgb[i] = rgb1[i] * (1.0 - val) + rgb2[i] * val;
  }
  return rgb;
}

// quick helper function to convert the array into something we can use for css
function colorToString(rgb) {
  return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
}
于 2012-07-31T08:39:36.093 に答える
1

私はあなたのために私自身のカラーファインダーを作成しました:

開始色と終了色、位置の数、現在の位置を受け入れ、CSSカラー値を返します。

それがフィドルで機能しているのを見てください

function restrict(value, low, high) {
    return value < low ? low : (value > high ? high : value);
}
function interpolate(rangeLow, rangeHigh, inputLow, inputHigh, value) {
    return (value - inputLow) / (inputHigh - inputLow) * (rangeHigh - rangeLow) + rangeLow;
}
function pad2(value) {
    return ('0' + value).substr(-2, 2);
}
function webColorFromRGB(color) {
    return '#' + pad2(color.R.toString(16)) + pad2(color.G.toString(16)) + pad2(color.B.toString(16));
}
function floatModulo(value, modulo) {
    return value - Math.floor(value / modulo) * modulo;
}
function webColorFromPosition(fromHue, toHue, positions, position) {
    var hue = interpolate(fromHue, toHue, 0, positions, position),
        RGB = {R: 0, G: 240, B: 120};
    for (var c in RGB) {
        if (!RGB.hasOwnProperty(c)) { continue; }
        RGB[c] = Math.round(interpolate(0, 255, 0, 360, restrict(Math.abs(floatModulo(hue + RGB[c], 360) * 6 - 1080) - 360, 0, 360)), 0);
    }
    return webColorFromRGB(RGB);
}

特徴:

  • 0から360までの開始色相と終了色相を受け入れます。
  • 終了は開始より低くすることができます。
  • 色相を複数回回転させたい場合は、360を超える値を使用できます

外観は次のとおりです。

エリックのカスタムカラーファインダー

于 2012-07-31T11:01:41.977 に答える
0

@BenTaitelbaumアプローチにいくつかの変更を加えました:

var fill = $("#progressFill");

var rgb = [],
    red = [255, 0, 0],
    green = [0, 255, 0],
    yellow = [255, 255, 0];

function color(val) {
    if (val < 0.5) {
        return colorToString(interpolate(val * 2, green, yellow));
    } else {
        return colorToString(interpolate((val - 0.5) * 2, yellow, red));
    }
}

function interpolate(val, rgb1, rgb2) {
    for (var i = 0; i < 3; i++) {
        rgb[i] = Math.floor(rgb1[i] * (1.0 - val) + rgb2[i] * val);
    }
    return rgb;
}

// quick helper function to convert the array into something we can use for css
function colorToString(rgb) {
    return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
}

$(function () {
    $({progress: 0}).animate({progress: 1}, {
        duration: 4000,
        step: function (now, fx) {
            fill.css({
                width: (now * 100) + "%",
                backgroundColor: color(now)
            });
        }
    });
});
于 2013-03-05T16:46:17.157 に答える