Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-83725

Improve the way activity icons are colorized

XMLWordPrintable

    • 5
    • HQ 2024 Sprint I4.2 Moppies

      In some versions, the activity icons are rendered in monochrome and colourized via CSS depending on the activity's purpose (communication, assessment, collaboration, etc.).

      However, how icons are colourized is complex and challenging to override by third-party themes. The filter used is generated using this codepen:

      https://codepen.io/sosuke/pen/Pjoqqp

      This means themes cannot use colour pickers to change their colour settings. The final result is not the exact colour but an approximation.

      This issue is about evaluating and implementing a different solution that allows themes to change the icon colors using only scss variables.

      One possible solution using SCSS mixins

      Someone found an alternative solution using SCSS mixins. It's a bit magic, but I tested it, and it works perfectly with a theme that extends Boost. The original code can be found https://jsfiddle.net/Tegos/3fchp0qm/ , and the original post where I found is https://stackoverflow.com/questions/61973473/sass-mixin-to-convert-a-hex-to-a-css-filter 

      The code I include in my theme SCSS is the following:

      This is only to define the variables in CSS and prevent the current filter from acting (this part can be simplified).

      // Original colors from Boost.
      $activity-icon-administration-bg: #da58ef;
      $activity-icon-assessment-bg: #f90086;
      $activity-icon-collaboration-bg: #5b40ff;
      $activity-icon-communication-bg: #eb6200;
      $activity-icon-content-bg: #0099ad;
      $activity-icon-interactivecontent-bg: #8d3d1b;// This is only to remove out current the Boost filters.
       
      // For the final issue all those filters should be removed.
      $activity-icon-administration-filter: '';
      $activity-icon-assessment-filter: '';
      $activity-icon-collaboration-filter: '';
      $activity-icon-communication-filter: '';
      $activity-icon-content-filter: '';
      $activity-icon-interactivecontent-filter: '';// Create an array of colors for the activities.
       
      // This is just to make it easier to change the colors in the future.
      $activity-purpose-colors: () !default;
      $activity-purpose-colors: map-merge(
          (
              "administration": $activity-icon-administration-bg,
              "assessment": $activity-icon-assessment-bg,
              "collaboration": $activity-icon-collaboration-bg,
              "communication": $activity-icon-communication-bg,
              "content": $activity-icon-content-bg,
              "interactivecontent": $activity-icon-interactivecontent-bg,
          ),
          $activity-purpose-colors
      ); 

      This is the magic mixin:

      // This is the magic mixin from https://jsfiddle.net/Tegos/3fchp0qm/
      @mixin recolor($color: #000, $opacity: 1) {
          $r: red($color) / 255;
          $g: green($color) / 255;
          $b: blue($color) / 255;
          $a: $opacity;    // grayscale fallback if SVG from data url is not supported
          $lightness: lightness($color);
          filter: saturate(0%) brightness(0%) invert($lightness) opacity($opacity);    // color filter
          $svg-filter-id: "recolor";
          filter: url('data:image/svg+xml;utf8,\
          <svg xmlns="http://www.w3.org/2000/svg">\
            <filter id="#{$svg-filter-id}" color-interpolation-filters="sRGB">\
              <feColorMatrix type="matrix" values="\
                0 0 0 0 #{$r}\
                0 0 0 0 #{$g}\
                0 0 0 0 #{$b}\
                0 0 0 #{$a} 0\
              "/>\
            </filter>\
          </svg>\
          ##{$svg-filter-id}');
      }

      This is how to apply the filter:

      // Here we apply the filter to the icons.
      // It is almost an exact copy of how we apply the
      // filter right now, but it could be simplfied.
      @each $type, $value in $activity-purpose-colors {
          .activityiconcontainer.#{$type} {
              &:not(.isbranded) {
                  .activityicon,
                  .icon {
                      &:not(.nofilter) {
                          @include recolor($value, 0.8);
                      }
                  }
              }
          }
      }

       

        1. screenshot-1.png
          62 kB
          Ferran Recio
        2. screenshot-2.png
          59 kB
          Ferran Recio
        3. (1) 4 Passed -- (Main)MDL-83725.png
          79 kB
          Kim Jared Lucas
        4. (1) 6 Passed -- (Main)MDL-83725.png
          74 kB
          Kim Jared Lucas

            tusefomal Ferran Recio
            tusefomal Ferran Recio
            Laurent David Laurent David
            Amaia Anabitarte Amaia Anabitarte
            Kim Jared Lucas Kim Jared Lucas
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 hour, 51 minutes
                1h 51m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.