Skip to content
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

convertcolors.js plugin: shorthex behavior is true even if set to false #933

Open
earthbound19 opened this issue Mar 29, 2018 · 11 comments
Open
Labels

Comments

@earthbound19
Copy link

How do I ensure svgo does not shorten hex color codes?

I'd like to know whether it is possible and how to change this option from a CLI parameter, but this is what I have tried which I believe has formerly worked in the convertColors.js config file to not convert e.g. ffffff hex color fill to fff; in the convertColors.js config file:

exports.params = {
    currentColor: false,
    names2hex: false,
    rgb2hex: true,
    shorthex: false,
    shortname: false
};

I believe the relevant setting there is shorthex: false,?

It has no effect one way or the other toggling it from true to false. Honestly I wonder if the config file is used at all. However I alter it, and even if I delete it, the conversion comes out the same.

@earthbound19 earthbound19 changed the title shorthex=false option ignored? shorthex: false option ignored? Mar 29, 2018
@GreLI
Copy link
Member

GreLI commented Mar 29, 2018

It should work:

// Convert long hex to short hex
if (params.shorthex && (match = val.match(regHEX))) {
val = '#' + match[0][1] + match[0][3] + match[0][5];
}

UPD.

Honestly I wonder if the config file is used at all.

That may be a reason. How do you pass a config?

@earthbound19
Copy link
Author

earthbound19 commented Mar 29, 2018

I believe the code section you cite does the opposite of what I want--I want to preserve long hex. I have tried commenting out that section of the config and it still converts to short hex.

If a config can be passed (the CLI options given by --help indicate it can be) I don't know how. I have tried passing --config=thisConfigFileInTheSamePath.js (with radically different options in the local config file) and seen no change in behavior.

@earthbound19
Copy link
Author

earthbound19 commented Mar 31, 2018

In my setup, and perhaps in the code base itself, something else is overriding the convertcolors.js plugin shorthex option and the code you cite is never called. I inserted a console.log debug statement there (in the code under // Convert long hex to short hex, and never see the console print, whether shorthex is set to true or false! I see other print statements just fine if I insert them at the start of the file.

I think what is overriding it is another plugin setting. I edited .svgo.yml to disable all plugins, and long hex colors remain long in output. I haven't yet bothered enabling plugins one by one to see which shortens long hex despite longhex being set to false in convertcolors.js.

@earthbound19 earthbound19 changed the title shorthex: false option ignored? convertcolors.js plugin: shorthex behavior is true even if set to false Mar 31, 2018
@m-steen
Copy link

m-steen commented May 21, 2021

I managed to narrow it down to the minifyStyles plugin. Apparently, that plugin also converts the colors. If you disable minifyStyles, the colors are left as is.
However, another problem surfaces: convertColors only converts colors in attributes, such as fill="#FF0000". Any colors inside a style attribute, such as style="fill:#FF0000" are left untouched.

@earthbound19
Copy link
Author

Thanks for narrowing that down.

Coincidentally the other day I reworked something in my toolchain to work with short hex also (which might really be what was going on when I opened this issue--I think another tool didn't work with short hex).

Still, if anyone else wants to avoid short hex, what you uncovered might help toward that.

@TrySound TrySound added the bug label Jan 2, 2022
@dsine-de
Copy link

dsine-de commented Nov 20, 2023

I don't use the minifyStyles plugin and it still doesn't work. If you add the shorthex: false parameter to the docs live preview (https://svgo.dev/docs/plugins/convert-colors/), it still outputs three digit hex colors.

@KTibow
Copy link
Contributor

KTibow commented Nov 20, 2023

If you use the original options it works fine.
image

@dsine-de
Copy link

dsine-de commented Nov 20, 2023

If you use the original options it works fine

It sometimes converts the color name to the 3 digit hex code even with shorthex: false:

image

@dsine-de
Copy link

It's not really a problem I think.

@KTibow
Copy link
Contributor

KTibow commented Nov 20, 2023

That's why you need to disable names2hex, probably it's hard coded as "black = #000"

@SethFalco
Copy link
Member

SethFalco commented Dec 2, 2023

How do I ensure svgo does not shorten hex color codes?

The option should work, I would assume the config may have been passed wrong for the version of SVGO you were using, or wasn't exporting correctly.

This config should work:

svgo.config.js

module.exports = {
  plugins: [
    {
      name: "preset-default",
      params: {
        overrides: {
          convertColors: {
            shorthex: false
          }
        }
      }
    }
  ]
}

Any colors inside a style attribute, such as style="fill:#FF0000" are left untouched.

Thanks for raising this, I'll work on it soon.

it still outputs three digit hex colors

That's because the shorthex option only stops converting long hex colors to short. Other colors may still become short hex as a result of other conversions.

If to convert 6 character hex colors to the 3 character equivalent where possible.

https://svgo.dev/docs/plugins/convert-colors/#parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants