如何在jquery中增加RGB颜色的R分量?

时间:2015-03-09 06:50:36

标签: jquery

1)这里我想获得rgb值。例如。 RGB(0,26,255)

$(document).on("click", "[id^='text']", function () {

            //debugger

            if ($(this).css('border-color') == 'rgb(0, 0, 255)') {
                $(this).css({ 'border-style': '', 'border-color': '' });
            }

            else {
                $(this).css({ 'border-style': 'solid', 'border-color': 'rgb(0, 0, 255)' });


                var rgb = $(this).css('background-color').replace(/^(rgb|rgba)\(/, '').replace(/\)$/, '').replace(/\s/g, '').split(',');



                $('#Rtxtbox').val((rgb[0] / 255) * 100);

                $('#Gtxtbox').val((rgb[1] / 255) * 100);

                $('#Btxtbox').val((rgb[2] / 255) * 100);



            }

        });

2)现在我试图将值从0增加到100%,但我只需要在亮度方面增加r分量

if (parseInt(rgb[0]) < 255) {


            R = Math.round(parseInt(rgb[0]) + 2.55);
            Rper = R / 255 * 100


            element.css('background-color', 'rgb(' + R + ',' + rgb[1] + ',' + rgb[2] + ')');

            $('#Rtxtbox').val(Math.round(Rper));

1 个答案:

答案 0 :(得分:1)

var rgb = $(this).css('background-color')
                 .match(/(\d+)/g)
                 .map(function(n) {return +n;}); // [0, 0, 255]

// Math.round(255 / 100) == 3;
if (rgb[0] < 253) { // 253 + 3 > 255
  rgb[0] += 3;
  element.css('background-color', 'rgb(' + rgb.join() + ')');
}