Hướng dẫn css get opposite color - css lấy màu ngược lại

Function to Invert Color of Element. Gets the luminosity of each and if they are close, inverts text color.

function adjustColor(element) {
    var style = window.getComputedStyle(element);
    var background = new Color(style['background-color']);
    var text = new Color(style['color']);
    if (Math.abs(background.luma - text.luma) < 100) {
        element.style.color = text.inverted.toString();
    }
}

The Color "Class" below. Accepts hex, rgb, rgba (even with percents), and can output to either one as well. Explorer will need polyfills for String.padStart and String.startsWith and the interpolated string in the toString() method will need to be modified using concat instead.

const Color = (function () {
    function toHex(num, padding) { return num.toString(16).padStart(padding || 2); }
    function parsePart(value) {
        var perc = value.lastIndexOf('%');
        return perc < 0 ? value : value.substr(0, perc);
    }
    function Color(data) {
        if (arguments.length > 1) {
            this[0] = arguments[0];
            this[1] = arguments[1];
            this[2] = arguments[2];
            if (arguments.length > 3) { this[3] = arguments[3]; }
        } else if (data instanceof Color || Array.isArray(data)) {
            this[0] = data[0];
            this[1] = data[1];
            this[2] = data[2];
            this[3] = data[3];
        } else if (typeof data === 'string') {
            data = data.trim();
            if (data[0] === "#") {
                switch (data.length) {
                    case 4:
                        this[0] = parseInt(data[1], 16); this[0] = (this[0] << 4) | this[0];
                        this[1] = parseInt(data[2], 16); this[1] = (this[1] << 4) | this[1];
                        this[2] = parseInt(data[3], 16); this[2] = (this[2] << 4) | this[2];
                        break;
                    case 9:
                        this[3] = parseInt(data.substr(7, 2), 16);
                    //Fall Through
                    case 7:
                        this[0] = parseInt(data.substr(1, 2), 16);
                        this[1] = parseInt(data.substr(3, 2), 16);
                        this[2] = parseInt(data.substr(5, 2), 16);
                        break;
                }
            } else if (data.startsWith("rgb")) {
                var parts = data.substr(data[3] === "a" ? 5 : 4, data.length - (data[3] === "a" ? 6 : 5)).split(',');
                this.r = parsePart(parts[0]);
                this.g = parsePart(parts[1]);
                this.b = parsePart(parts[2]);
                if (parts.length > 3) { this.a = parsePart(parts[3]); }
            }
        }
    }
    Color.prototype = {
        constructor: Color,
        0: 255,
        1: 255,
        2: 255,
        3: 255,
        get r() { return this[0]; },
        set r(value) { this[0] = value == null ? 0 : Math.max(Math.min(parseInt(value), 255), 0); },
        get g() { return this[1]; },
        set g(value) { this[1] = value == null ? 0 : Math.max(Math.min(parseInt(value), 255), 0); },
        get b() { return this[2]; },
        set b(value) { this[2] = value == null ? 0 : Math.max(Math.min(parseInt(value), 255), 0); },
        get a() { return this[3] / 255; },
        set a(value) { this[3] = value == null ? 255 : Math.max(Math.min(value > 1 ? value : parseFloat(value) * 255, 255), 0); },
        get luma() { return .299 * this.r + .587 * this.g + .114 * this.b; },
        get inverted() { return new Color(255 - this[0], 255 - this[1], 255 - this[2], this[3]); },
        toString: function (option) {
            if (option === 16) {
                return '#' + toHex(this.r) + toHex(this.g) + toHex(this.b) + (this[3] === 255 ? '' : toHex(this[3]));
            } else if (option === '%') {
                if (this.a !== 1) {
                    return `rgba(${this.r / 255 * 100}%, ${this.b / 255 * 100}%, ${this.g / 255 * 100}%, ${this.a / 255})`;
                } else {
                    return `rgb(${this.r / 255 * 100}%, ${this.b / 255 * 100}%, ${this.g / 255 * 100})%`;
                }
            } else {
                if (this.a !== 1) {
                    return `rgba(${this.r}, ${this.b}, ${this.g}, ${this.a})`;
                } else {
                    return `rgb(${this.r}, ${this.b}, ${this.g})`;
                }
            }
        }
    };

    return Color;
}());

RGB (0,255,0)

from PIL import Image  

def complementaryColor(hex):
    """Returns complementary RGB color

    Example:
    >>>complementaryColor('FFFFFF')
    '000000'
    """
    if hex[0] == '#':
        hex = hex[1:]
    rgb = (hex[0:2], hex[2:4], hex[4:6])
    comp = ['02%X' % (255 - int(a, 16)) for a in rgb]
    return comp.join()

Thí dụ

def blackwhite(my_hex):
    """Returns complementary RGB color

    Example:
    >>>complementaryColor('FFFFFF')
    '000000'
    """
    if my_hex[0] == '#':
        my_hex = my_hex[1:]
    rgb = (my_hex[0:2], my_hex[2:4], my_hex[4:6])
    comp = ['%X' % (0 if (15 - int(a, 16)) <= 7 else 15) for a in rgb]
    return ''.join(comp)


print blackwhite('#36190D')

Màu sắcJul 20, 2016 at 10:13

RGBAnay Bose

màu xanh lá1 gold badge14 silver badges23 bronze badges

4

RGB (0,0,255)RRGGBB.

def complementaryColor(my_hex):
    """Returns complementary RGB color

    Example:
    >>>complementaryColor('FFFFFF')
    '000000'
    """
    if my_hex[0] == '#':
        my_hex = my_hex[1:]
    rgb = (my_hex[0:2], my_hex[2:4], my_hex[4:6])
    comp = ['%02X' % (255 - int(a, 16)) for a in rgb]
    return ''.join(comp)

Màu xanh da trời


Hãy tự mình thử »

RGB (0,0,255)

Các sắc thái của màu xám thường được xác định bằng cách sử dụng các giá trị bằng nhau cho tất cả 3 nguồn ánh sáng:Jul 20, 2016 at 10:26

RGB (0,0,0)Moses Koledoye

Màu đen8 gold badges125 silver badges133 bronze badges

4

& nbsp;

>>> from colorir import SwatchPalette
>>> colors = SwatchPalette.new_complementary(2, color="ff0000")
>>> colors[1]  # Get the complementary of red
HexRGB(#00ffff)

RGB (255,0,0)

RGB (0,255,0)  color: blue; /* Blue text color */  border: 10px solid currentcolor; /* Blue border color */ }May 19 at 19:01

Tôi đang cố gắng tìm ra màu bổ sung của một màu nhất định bằng cách sử dụng Python. Đây là mã của tôi. Mã trả về thông báo lỗi cho biết "ApertyError: 'list' đối tượng không có thuộc tính 'tham gia'" Tôi cần một gợi ý. Ngoài ra, có thể có một mã mạnh mẽ hơn, tính toán màu đối diện/bổ sung, mà về cơ bản tôi đang tìm kiếm. Đề xuất của bạn sẽ hữu ích.


một chức năng tương tự khác

màu xanh lá


RGB (0,0,255)RRGGBB.

Màu xanh da trời

  • Hãy tự mình thử »
  • RGB (0,0,255)
  • Các sắc thái của màu xám thường được xác định bằng cách sử dụng các giá trị bằng nhau cho tất cả 3 nguồn ánh sáng:
  • RGB (0,0,0)
  • Màu đen
  • & nbsp;

RGB (255,0,0)

RGB (0,255,0)  color: blue; /* Blue text color */  border: 10px solid currentcolor; /* Blue border color */ }

Tôi đang cố gắng tìm ra màu bổ sung của một màu nhất định bằng cách sử dụng Python. Đây là mã của tôi. Mã trả về thông báo lỗi cho biết "ApertyError: 'list' đối tượng không có thuộc tính 'tham gia'" Tôi cần một gợi ý. Ngoài ra, có thể có một mã mạnh mẽ hơn, tính toán màu đối diện/bổ sung, mà về cơ bản tôi đang tìm kiếm. Đề xuất của bạn sẽ hữu ích.RED , GREEN , BLUE ).

một chức năng tương tự khác

Hỏi ngày 20 tháng 7 năm 2016 lúc 10:13Jul 20, 2016 at 10:13

Thí dụ

Màu sắcRGBMàu sắc
RGB & nbsp; RGB (255,0,0)
RGB & nbsp; RGB (255,0,0)
RGB & nbsp; RGB (255,0,0)

Màu đỏ

RGB (0,255,0)

Thí dụ

Màu sắcRGBMàu sắc
RGB & nbsp; RGB (255,0,0)
RGB & nbsp; RGB (255,0,0)
RGB & nbsp; RGB (255,0,0)

Màu đỏ



RGB (0,255,0)

Thí dụ

Màu sắcRRGGBB.

RGB

màu xanh lá

Thí dụ

Màu sắcRGBRGBMàu sắc
RGB & nbsp; & nbsp; RGB (255,0,0)
RGB & nbsp; & nbsp; RGB (255,0,0)
RGB & nbsp; & nbsp; RGB (255,0,0)

Màu đỏ

RGB (0,255,0)

Thí dụ

Màu sắcRGBRGBMàu sắc
RGB & nbsp; & nbsp; RGB (255,0,0)
RGB & nbsp; & nbsp; RGB (255,0,0)
RGB & nbsp; & nbsp; RGB (255,0,0)

Màu đỏ


RGB (0,255,0)

Thí dụ

Màu sắc


RGB

màu xanh lá

RGB (0,0,255)RRGGBB.

Màu xanh da trờiHãy tự mình thử »Màu sắc
RGB & nbsp; RGB
& nbsp; RGB (255,0,0) RGB
& nbsp; RGB (255,0,0) RGB
& nbsp; RGB (255,0,0) RGB
& nbsp; RGB (255,0,0) RGB
& nbsp; RGB (255,0,0) RGB
& nbsp; RGB (255,0,0) RGB

& nbsp;

Màu đỏ

Thí dụ

Màu đỏ

RGB (0,255,0)
  color: blue; /* Blue text color */
  border: 10px solid currentcolor; /* Blue border color */
}

Màu đỏ