New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors should be written as hex codes instead of rgb functions #28

Closed
agabrys opened this Issue Jan 30, 2018 · 2 comments

Comments

2 participants
@agabrys
Member

agabrys commented Jan 30, 2018

Value #fff is converted to rgb(255, 255, 255). Hex codes are more concise and readable than rgb functions so it would be great if the plugin can use them.

CSS parser allows to set CSSFormat#setRgbAsHex to true and then:

  • #fff#ffffff
  • #ffffff#ffffff
  • rgb(255, 255, 255)#ffffff
  • rgba(255, 255, 255, 0.5)rgba(255, 255, 255, 0.5)
@Smasherr

This comment has been minimized.

Smasherr commented Jan 30, 2018

I would say colors should be kept as-is. If developer want to have a color as hex, keep it as hex. If he defines it as a rgb function, stick to it.

@agabrys

This comment has been minimized.

Member

agabrys commented Jan 30, 2018

Colors are stored in the RGBColorImpl class. The getCssText method allows to return value in hex or rgb. There aren't any possibility to copy them "as is". I can use hex codes for now. We need to create an improvement task in CSS Parser library to allow use original values.

/*
 * Copyright (C) 1999-2018 David Schweinsberg.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.steadystate.css.dom;

import java.io.Serializable;

import org.w3c.css.sac.LexicalUnit;
import org.w3c.dom.DOMException;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.w3c.dom.css.RGBColor;

import com.steadystate.css.format.CSSFormat;
import com.steadystate.css.format.CSSFormatable;

/**
 * Implementation of {@link RGBColor}.
 *
 * @author <a href="mailto:davidsch@users.sourceforge.net">David Schweinsberg</a>
 * @author rbri
 */
public class RGBColorImpl implements RGBColor, CSSFormatable, Serializable {

    private static final long serialVersionUID = 8152675334081993160L;
    private CSSPrimitiveValue red_;
    private CSSPrimitiveValue green_;
    private CSSPrimitiveValue blue_;

    /**
     * Constructor that reads the values from the given
     * chain of LexicalUnits.
     * @param lu the values
     * @throws DOMException in case of error
     */
    public RGBColorImpl(final LexicalUnit lu) throws DOMException {
        LexicalUnit next = lu;
        red_ = new CSSValueImpl(next, true);
        next = next.getNextLexicalUnit();   // ,
        if (next != null) {
            if (next.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
                // error
                throw new DOMException(DOMException.SYNTAX_ERR,
                    "rgb parameters must be separated by ','.");
            }
            next = next.getNextLexicalUnit();
            if (next != null) {
                green_ = new CSSValueImpl(next, true);
                next = next.getNextLexicalUnit();   // ,
                if (next != null) {
                    if (next.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
                        // error
                        throw new DOMException(DOMException.SYNTAX_ERR,
                            "rgb parameters must be separated by ','.");
                    }
                    next = next.getNextLexicalUnit();
                    blue_ = new CSSValueImpl(next, true);
                    next = next.getNextLexicalUnit();
                    if (next != null) {
                        // error
                        throw new DOMException(DOMException.SYNTAX_ERR,
                            "Too many parameters for rgb function.");
                    }
                }
            }
        }
    }

    /**
     * Constructor.
     * The values for the colors are null.
     */
    public RGBColorImpl() {
        super();
    }

    /**
     * Returns the red part.
     */
    public CSSPrimitiveValue getRed() {
        return red_;
    }

    /**
     * Sets the red part to a new value.
     * @param red the new CSSPrimitiveValue
     */
    public void setRed(final CSSPrimitiveValue red) {
        red_ = red;
    }

    /**
     * Returns the green part.
     */
    public CSSPrimitiveValue getGreen() {
        return green_;
    }

    /**
     * Sets the green part to a new value.
     * @param green the new CSSPrimitiveValue
     */
    public void setGreen(final CSSPrimitiveValue green) {
        green_ = green;
    }

    /**
     * Returns the blue part.
     */
    public CSSPrimitiveValue getBlue() {
        return blue_;
    }

    /**
     * Sets the blue part to a new value.
     * @param blue the new CSSPrimitiveValue
     */
    public void setBlue(final CSSPrimitiveValue blue) {
        blue_ = blue;
    }

    /**
     * Same as {@link #getCssText(CSSFormat)} but using the default format.
     *
     * @return the formated string
     */
    public String getCssText() {
        return getCssText(null);
    }

    /**
     * {@inheritDoc}
     */
    public String getCssText(final CSSFormat format) {
        final StringBuilder sb = new StringBuilder();
        if (null != format && format.isRgbAsHex()) {
            sb
                .append("#")
                .append(getColorAsHex(red_))
                .append(getColorAsHex(green_))
                .append(getColorAsHex(blue_));
            return sb.toString();
        }

        sb
            .append("rgb(")
            .append(red_)
            .append(", ")
            .append(green_)
            .append(", ")
            .append(blue_)
            .append(")");
        return sb.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return getCssText(null);
    }

    private String getColorAsHex(final CSSPrimitiveValue color) {
        return String.format("%02x", Math.round(color.getFloatValue(LexicalUnit.SAC_INTEGER)));
    }
}

@agabrys agabrys added this to the 2.0.1 milestone Mar 18, 2018

agabrys added a commit that referenced this issue Mar 18, 2018

#28 Colors should be written as hex codes instead of rgb functions
- use hex representation instead of rgb
- use CssFormatter to convert charset rules and selectors to strings

agabrys added a commit that referenced this issue Mar 18, 2018

#28 Colors should be written as hex codes instead of rgb functions
- use hex representation instead of rgb
- use CssFormatter to convert charset rules and selectors to strings

@agabrys agabrys closed this Mar 18, 2018

agabrys added a commit that referenced this issue Mar 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment