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

Update advanced preview and sliders on color change #61

Open
Jaza opened this issue Sep 10, 2014 · 2 comments
Open

Update advanced preview and sliders on color change #61

Jaza opened this issue Sep 10, 2014 · 2 comments

Comments

@Jaza
Copy link

Jaza commented Sep 10, 2014

Currently, when you update the hex value in the text box, only the ".current-color" preview box changes accordingly. Nothing in the advanced tab gets updated (assuming that showAdvanced is enabled). Ideally, everything in the advanced tab should change to the new color on change (i.e. the advanced preview box, and the HSL sliders).

@SKeehnen
Copy link

Hi Jaza,

I have been playing around with a fix to update the sliders, it might not be the best solution, but it works for me:

               updatePreview: function ($thisEl) {
            function updatePreviewElements(color) {
                // Update "current-color" preview square.
                $thisEl.siblings(".input-group-btn")
                       .find(".current-color")
                       .css("background-color", "#" + color);

                // Update preview and HSL sliders in advanced tab.
                if (settings.showAdvanced) {
                    $thisEl.parent()
                           .find(".advanced-content .color-preview")
                           .css("background-color", "#" + color);

                    var clr = tinycolor(color);
                    var colorHsl = clr.toHsl(); // { h: 0, s: 1, l: 0.5, a: 1 }
                    settings.advancedStatus = colorHsl;

                    var lenght = $thisEl.parent().find("span.spectrum-lightness").width();
                    var sliderPosH = lenght / 360 * colorHsl.h;
                    var sliderPosL = lenght * colorHsl.l;
                    var sliderPosS = lenght * colorHsl.s;

                    $thisEl.parent()
                        .find(".spectrum-hue .highlight-band")
                 .css("left", sliderPosH + "px");
                    $thisEl.parent()
                         .find("span.hue-value").empty().append(parseInt(colorHsl.h));

                    $thisEl.parent()
                           .find(".spectrum-lightness .highlight-band")
                    .css("left", sliderPosL + "px");
                    $thisEl.parent()
                         .find("span.lightness-value").empty().append(parseInt(colorHsl.l * 100) + "%");


                    $thisEl.parent()
                        .find(".spectrum-saturation .highlight-band")
                 .css("left", sliderPosS + "px");
                    $thisEl.parent()
                         .find("span.saturation-value").empty().append(parseInt(colorHsl.s * 100) + "%");

                    // midpoint of the current left position of the color band
                    var hbWidth = $(".highlight-band").first().outerWidth(),
                        halfHBWidth = hbWidth / 2,
                        highlightBandLocation = parseInt($thisEl.css("left"), 10) + halfHBWidth,
                        spectrumType = "bidirectional",
                        colorMultiplier = methods.getColorMultiplier(spectrumType, highlightBandLocation, "advanced");

                    methods.updateSaturationStyles($thisEl.parent().find(".spectrum-saturation"), colorHsl.h, colorHsl.l);
                    methods.updateHueStyles($thisEl.parent().find(".spectrum-hue"), colorHsl.s, colorHsl.l);
                    methods.updateLightnessStyles($thisEl.parent().find(".spectrum-lightness"), colorHsl.h, colorHsl.s);
                    methods.modifyHighlightBand($thisEl.parent().find(".highlight-band"), colorMultiplier, spectrumType);

                }
            }

            if (!settings.allowBlank) {
                myColorVars.typedColor = tinycolor($thisEl.val()).toHex();
                updatePreviewElements(myColorVars.typedColor);
            } else {
                myColorVars.typedColor = $thisEl.val().match(/^\s+$|^$/) ? '' : tinycolor($thisEl.val()).toHex();
                if (myColorVars.typedColor === '') {
                    $thisEl.siblings(".input-group-btn")
                           .find(".current-color")
                           .css("background", "none");
                } else {
                    updatePreviewElements(myColorVars.typedColor);
                }
            }
        },

@charleslaw
Copy link

I have a solution I'm happy with:#81

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

No branches or pull requests

3 participants