First off, color.module is getting better all the time - go team!

I discovered a drawback this morning - a new theme like Bartik needs more than three divs, a header and a paragraph to show how it actually makes use of color.module in the preview area.

If a theme wants to override its preview HTML, the only way to do so is to override the color_scheme_form theme function and change a single line of string concatenation.

I propose the following patch, which is completely backwards compatible with any theme that already uses color.module. It allows for a theme to include a /color/preview.html file inside it to be shown when the color.module preview shows.

I think this simple, backwards-compatible change should continue to help color.module evolve. I'd love to hear your feedback.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Steven Merrill’s picture

Steven Merrill’s picture

New patch with comment language cleanup.

Steven Merrill’s picture

Status: Active » Needs review

Testbot?

sexyhalibut’s picture

noob here, but inspired to review by @hedgemedge

Patch works for me, in that adding a preview.html does, in fact, change the behavior.

jensimmons’s picture

+57. The current implementation of preview html and dummy text is hardcoded into the module. This makes it overwriteable. Better Drupal practice, for sure. And another step to making color module useful to any theme.

chx’s picture

Status: Needs review » Needs work

One, add a DRUPAL_ROOT . '/' . to drupal_get_path, two, I strongly think that this should be moved to a separate HTML and the hardwired removed so it's self documenting and easy. Maybe in misc directory?

Steven Merrill’s picture

Status: Needs work » Needs review
FileSize
2.43 KB

@chx: Good call on using DRUPAL_ROOT and on generalizing this out to a file. Thanks!

I've moved the default preview HTML to /misc/color-module-preview.html and also slightly modified Garland's preview.css so that we don't have to use PHP to assemble the path to the preview image as it was with the previous hardcoded HTML.

Garland still very happily recolors with this patch: http://skitch.com/00sven/n94yj/appearance-bartik.local .

Steven Merrill’s picture

Also worth noting: modifying the theme function for color_scheme_form in your own theme isn't even an option, since the admin theme (likely Seven) is the one that's active.

Steven Merrill’s picture

Another new patch. The missing link is that the color.module preview is entirely done through JavaScript and so changing the preview HTML will also require supplying a new JavaScript function in the namespace Drupal.color.callback which would be loaded from the location specified at array key 'preview_js' in the theme's color.inc.

As before, if the defaults are used, nothing else must be changed (as in Garland.)

A sample callback (in progress) for Bartik would then look something like this:

/* $Id$ */

(function ($) {
  Drupal.color = {
    callback: function(context, settings, form) {
      // Solid background.
      $('#preview', form).css('backgroundColor', $('#palette input[name="palette[base]"]', form).val());
    
      // Text preview
      $('#preview #preview-main h2, #preview #preview-main p', form).css('color', $('#palette input[name="palette[text]"]', form).val());
      $('#preview #preview-content a', form).css('color', $('#palette input[name="palette[link]"]', form).val());
      
      // CSS3 Gradient
      var gradient_start = $('#palette input[name="palette[top]"]', form).val();
      var gradient_end = $('#palette input[name="palette[bottom]"]', form).val();
    
      $('#preview #preview-header', form).attr('style', "background-color: " + gradient_start + "; background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(" + gradient_start + "), to(" + gradient_end + ")); background-image: -moz-linear-gradient(-90deg, " + gradient_start + ", " + gradient_end + ");");
    }
  };
})(jQuery);
EvanDonovan’s picture

This looks brilliant. I'll try to test tonight, if I don't fall asleep first.

Steven Merrill’s picture

One more patch attached.

Here's the summary thus far. Like many other things in color.module's design, assumptions were made about the preview given by color.module.

First off, it assumed that the following HTML:

<div id="preview">
  <div id="text">
    <h2>Lorem ipsum dolor</h2>
    <p>Sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud <a href="#">exercitation ullamco</a> laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  </div>
  <div id="img"></div>
</div>

and the following JavaScript:

// Solid background.
$('#preview', form).css('backgroundColor', $('#palette input[name="palette[base]"]', form).val());

// Text preview
$('#text', form).css('color', $('#palette input[name="palette[text]"]', form).val());
$('#text a, #text h2', form).css('color', $('#palette input[name="palette[link]"]', form).val());

// Set up gradients if there are some.
var color_start, color_end;
for (i in settings.gradients) {
  color_start = farb.unpack($('#palette input[name="palette[' + settings.gradients[i]['colors'][0] + ']"]', form).val());
  color_end = farb.unpack($('#palette input[name="palette[' + settings.gradients[i]['colors'][1] + ']"]', form).val());
  if (color_start && color_end) {
    var delta = [];
    for (j in color_start) {
      delta[j] = (color_end[j] - color_start[j]) / (settings.gradients[i]['vertical'] ? height[i] : width[i]);
    }
    var accum = color_start;
    // Render gradient lines.
    $('#gradient-' + i + ' > div', form).each(function () {
      for (j in accum) {
        accum[j] += delta[j];
      }
      this.style.backgroundColor = farb.pack(accum);
    });
  }
}

... would always be enough to render a suitable preview of what a colored theme would look like.

This patch:

  • Breaks out the previously hardcoded preview HTML into an HTML file
  • Allows themes to specify their own preview HTML file if not using the default
  • Allows themes to specify their own preview JS function if not using the default
  • Moves the default preview HTML and preview JavaScript files into files in color.module
  • Maintains backwards compatibility with Garland and other existing themes that use color.module

And I'll be honest, here's what I don't like about it thus far:

  • At the moment, since the callback function to recolor the preview can't be defined as a closure, it has to accept a whopping 6 arguments, many of which may not be used. (i.e. function(context, settings, form, farb, height, width) {})
ksenzee’s picture

I wouldn't worry about the six arguments. The right way to do this would be to rewrite color.module's JS to use prototypal inheritance, instantiate an object, bake the six parameters into the object as properties, define Drupal.color.callback as a method on that object, and use jQuery.proxy to bind the instantiated object to the method. That is entirely out of scope for Drupal 7, so I think six arguments are a fine compromise. :)

Steven Merrill’s picture

If this patch goes in, themers not only get to color with more than 5 colors, they can also show truer-to-life previews such as this one:

Appearance | bartik.local

jensimmons’s picture

Status: Reviewed & tested by the community » Needs review

For those concerned about this patch being worked on now, read webchick's comments about modifications to color module at:
http://drupal.org/node/693504#comment-2565206

Also — this is where we are at:
1) All new core themes must support color module. [#769692] http://drupal.org/node/769692
2) Color module was written for Garland and does not properly work with any new themes being created for core.

So.... we either must alter color module in the simplest way possible, or drop color module support from the new themes and stop requiring it.

chx’s picture

Status: Needs review » Reviewed & tested by the community

For the record, I have told jensimmons the only reasons I did not RTBC this because it's a feature. But given the comment she linked I am actually happy to RTBC this

mradcliffe’s picture

Status: Needs review » Reviewed & tested by the community

I tested this visually with the Garland theme. The preview.png changed consistently using the color picker GUI.

When saved, the header top/bottom gradient of the preview.png was different from the actual header top/bottom gradient. This was the same with or without the patch.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

This is treading on the border of feature-y, but Steven and Jen have done a good job of explaining why this is needed in order to support themes other than Garland with fancier divs and spans and whatnot. Also? It's color module. :P~

Committed to HEAD. :)

EvanDonovan’s picture

Just as a note, this patch doesn't break the preview in the Corolla theme, so that's good. The Corolla theme just doesn't have a preview.html or preview.css yet to take advantage of it.

dcrocks’s picture

The latest busy(4/21) gets an error msg

Notice: Undefined index: base in _color_rewrite_stylesheet() (line 433 of /Library/WebServer/Documents/drdev422/modules/color/color.module).

It does this both before and after the patch in #11. But the preview seems to be messed up after the patch.

colors 'base', 'link', and 'text' are hard coded key words in color.module. Busy seems to be using the color 'base' in a different manner than is perhaps intended.

I'm not sure if this is the right place to post this, and it may all be a 'busy' problem, but the rendering of the preview for busy did change after the patch.

Steven Merrill’s picture

One interesting note about 'base' - the 'base' color must _not_ appear exactly in the colored part of the stylesheet, and this has not been altered by either of my color.module patches (#778880: Add static caching to color.module or #776684: The color.module preview HTML is hardcoded.) I also don't believe that the third big color.module patch (#693504: Color.module does not support more than 5 colors and has hard-coded labels) changed it, but I didn't have much of a hand in that one.

Since 'base' is actually only used to color-shift items that are not explicitly included in the color.inc array or the pngs specified in color.inc (and since Bartik uses neither of these,) I actually removed 'base' from Bartik's color.inc altogether. (Check out the color.inc changes to Bartik in http://drupal.org/node/762474#comment-2879434 .)

This might do better as a new issue or as a follow-up to #693504: Color.module does not support more than 5 colors and has hard-coded labels.

dcrocks’s picture

You're right. I'll look some more and move to another issue if I get a better handle on this.

dcrocks’s picture

Well, never mind. Busy's style sheet did have a color ref identical to the color specified by 'base' in color.inc. A tiny change fixed that. And its preview.css needed the same 'background-image' change that you added to garland. I'll report it over in busy's issue queue.

Status: Fixed » Closed (fixed)
Issue tags: -Bartik

Automatically closed -- issue fixed for 2 weeks with no activity.