rusackas commented on code in PR #42910:
URL: https://github.com/apache/superset/pull/42910#discussion_r4416066697


##########
superset/mcp_service/chart/chart_utils.py:
##########
@@ -3844,6 +3854,15 @@ def merge_bullet_form_data(
     for values_key, labels_key in dependent_controls.items():
         if values_key in new_form_data and labels_key not in new_form_data:
             new_form_data[labels_key] = ""
+    # Per-band colors are applied by position, so replacing the thresholds
+    # without colors must not re-apply the old colors to the new bands. 
Repeating
+    # the saved thresholds leaves the bands unchanged, so their colors are 
kept.
+    if (
+        "ranges" in new_form_data
+        and "range_colors" not in new_form_data
+        and new_form_data["ranges"] != existing_form_data.get("ranges")

Review Comment:
   Good catch. The comparison now tokenizes both sides the way Explore does 
(trims, drops blanks, numeric compare), so `50, 100`, `50.0,100` and a trailing 
comma keep their `range_colors`. Added a test.



##########
superset-frontend/src/explore/components/controls/IntervalColorsControl/legacyColors.ts:
##########
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+  getCategoricalSchemeRegistry,
+  validateNumber,
+} from '@superset-ui/core';
+
+export const parseIntervalBounds = (intervals?: string): number[] =>
+  (intervals ?? '')
+    .split(',')
+    .map(part => part.trim())
+    .filter(part => part !== '')
+    .map(Number)
+    .filter(bound => Number.isFinite(bound));
+
+/**
+ * Resolves the legacy `interval_color_indices` control (comma-separated,
+ * 1-indexed positions into the chosen categorical color scheme) into real
+ * hex colors, one per interval band (`boundCount` entries). Shared by the
+ * control panel (display-only fallback while a legacy chart is being edited)
+ * and by `handleDeprecatedControls` (which, on request, migrates the value
+ * into `interval_colors`).

Review Comment:
   Applied the docstring suggestion.



##########
superset-frontend/src/explore/components/controls/ColorPickerControl.tsx:
##########
@@ -193,10 +234,25 @@ export default function ColorPickerControl({
     const rgb = color.toRgb();
     const hex = rgbaToHex(rgb).toLowerCase();
 
+    // Match against the same hex the preset chip and trigger display, so a
+    // special color picked from a theme-resolved chip maps back to its key.
+    // Only keys the caller actually offered as presets are candidates;
+    // otherwise an ordinary theme swatch that shares a hex with Green/Red
+    // (e.g. `colorSuccess`) would be misread as the special key.
+    const offeredSpecialKeys = new Set(
+      (customPresets ?? []).flatMap(item =>
+        item.colors.filter(color => color in SPECIAL_COLORS),
+      ),
+    );
     const specialEntry = resolveThemeTokens
-      ? Object.entries(SPECIAL_COLORS).find(
-          ([, rgba]) => rgbaToHex(rgba).toLowerCase() === hex,
-        )
+      ? Object.keys(SPECIAL_COLORS)
+          .filter(key => offeredSpecialKeys.has(key))
+          .map(key => [key, toDisplayHex(key, themeColors, true)] as const)
+          .find(
+            ([, displayHex]) =>
+              displayHex !== undefined &&
+              normalizeColorToHex(displayHex) === hex,

Review Comment:
   @sadpandajoe `normalizeColorToHex` now expands 3/4-digit shorthand before 
comparing, so Green resolves under a `#0f0` theme. Added a test selecting Green 
with `colorSuccess: '#0f0'`.



##########
superset-frontend/plugins/plugin-chart-echarts/src/Bullet/transformProps.ts:
##########
@@ -103,23 +130,50 @@ export default function transformProps(
     theme.colorFillSecondary,
     theme.colorFill,
   ];
+  // Custom colors (if any) are authored positionally against the original,
+  // pre-sort `ranges` order via the `range_colors` control -- capture each
+  // range's color here, before the descending sort below reorders them.
   const sortedRanges = [...ranges]
-    .map((value, i) => ({ value, label: rangeLabels[i] }))
+    .map((value, i) => ({
+      value,
+      label: rangeLabels[i],
+      // Colors only apply to user-entered ranges; the automatic fallback band
+      // must not pick up a stale `range_colors` entry kept from earlier edits.
+      color: explicitRanges ? rangeColors?.[i] || undefined : undefined,
+    }))
     .filter(({ value }) => value > axisMin)
     .sort((a, b) => b.value - a.value);
   // Each band's label sits inside its own top-right corner: the visible strip
   // of a nested band ends at its threshold, so the label lands inside the
   // range it names and the rightmost label cannot clip at the chart edge.
-  const bands = sortedRanges.map(({ value, label }, i) => ({
+  const getBandLabelColor = (fill?: string): string => {
+    const luminance = fill ? getColorLuminance(fill) : null;
+    if (luminance === null) return theme.colorTextSecondary;
+    // Fixed light/dark foregrounds, not `theme.colorText`: that token flips
+    // to a light color in dark themes and would vanish on a light custom
+    // fill. Pick whichever foreground actually contrasts better against
+    // this fill, rather than a single magic luminance cutoff that doesn't
+    // match where white and the dark foreground really break even.
+    return wcagContrast(luminance, BAND_LABEL_LUMINANCE.light) >=
+      wcagContrast(luminance, BAND_LABEL_LUMINANCE.dark)
+      ? BAND_LABEL_COLORS.light
+      : BAND_LABEL_COLORS.dark;
+  };
+  const bands = sortedRanges.map(({ value, label, color }, i) => ({
     name: label,
     itemStyle: {
-      color: bandFills[Math.min(i, bandFills.length - 1)],
+      // A custom `range_colors` entry wins; otherwise fall back to the
+      // default theme-token ramp exactly as before this control existed, so
+      // Bullet charts saved without `range_colors` are unaffected.
+      color: color || bandFills[Math.min(i, bandFills.length - 1)],
     },
     label: {
       show: Boolean(showLabels) && Boolean(label),
       position: 'insideTopRight',
       distance: theme.sizeUnit,
-      color: theme.colorTextSecondary,
+      // Labels sit inside the band, so a custom fill needs a foreground that
+      // contrasts with it; theme-ramp fills keep the default label color.
+      color: getBandLabelColor(color),

Review Comment:
   @sadpandajoe Good point. An uncolored band's label now contrasts against the 
nearest outer custom fill, so "Low" over a black outer band renders light. 
Added a test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to