Skip to content

Implement CSS Palette support

Thomas K/BIDI requested to merge tomkb/krita:tombk/CSS_Palette_Support into master

I add the possibility to import CSS palette in Krita, in the same way as the .gpl ones. Those palettes can be generated by GIMP or created by the user, but the only property considered is "color".

The following color format is supported (based on their CSS syntax) :

  • rgb
  • rbga
  • hsl
  • hsla
  • hexadecimal

The file's suffix must be ".css". If there is an error in the syntax or the color definition, errors will be raised and the palette loading aborted.

I add the following elements :

  • The ".css" suffix in the supported formats for palette import.
  • A loadCss() method, robust, to read css palettes
    • Handle comments
    • Handle multiple properties, but consider only the "color" property
    • With a bit of lenient parsing (e.g. : "rgb (255, 255, 255)" is accepted)

I used tools that are already present in the project, like regular expression through Qt.

Test Plan

To test the change I made you can :

  • Generate ".css" files (with GIMP or by using those that i will copy under these steps)
  • Import a new palette
  • You should see the ".css" palette files appeared when using the supported formats (in the file explorer)
  • Load the palette
  • Palette should be loaded

Wrong palettes should generate bugs, but they will not crash Krita.

Palettes (short) examples :

.a {color: rgba(197, 12, 12, 0.705)}
.b {color: rgb(14, 186, 14)}
.c {color: hsl(273, 70%, 46%)}
.d {color: hsla(334, 98%, 47%, 0.938)}
.e {color: #04f1d5}
.f {color: #4f04f147}
.color1 {background-color : #ff0000; color: #08bfff;}
.color2 {color: rgb(172, 27, 201); background-color : rgb(255, 0, 0);}
.color3 {background-color : red; color: rgb(255, 185, 216); text-align : center}
.color4 {color: hsl(0, 86%, 37%); text-align : center; text-align : center;  text-align : center;}
/* Various cases of commentary */
.a { color: rgb(0,0,0) }
.b { color: /***** test comment *******/ rgb(120,0, 16) }
.c { color: rgb/*the 
                 lie*/(120,0, 16); }
.d { color: #05f829; }
. { color : hsla(140, 100%, 35%, 0.795) } /*no name case*/
/* This one should generate an error */
a { color: rgb(0,0,0) }

Formalities Checklist

  • I confirmed this builds.
  • I confirmed Krita ran and the relevant functions work.
  • I tested the relevant unit tests and can confirm they are not broken. (If not possible, don't hesitate to ask for help!)
  • I made sure my commits build individually and have good descriptions as per KDE guidelines.
  • I made sure my code conforms to the standards set in the HACKING file.
  • I can confirm the code is licensed and attributed appropriately, and that unattributed code is mine, as per KDE Licensing Policy.

Reminder: the reviewer is responsible for merging the patch, this is to ensure at the least two people can build the patch. In case a patch breaks the build, both the author and the reviewer should be contacted to fix the build. If this is not possible, the commits shall be reverted, and a notification with the reasoning and any relevant logs shall be sent to the mailing list,

Edited by Thomas K/BIDI

Merge request reports