-
Notifications
You must be signed in to change notification settings - Fork 887
EuiColorStops: Feature Stepped ColorStops #4613
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
Changes from 7 commits
1400b4c
53065b7
f1d9027
7bbd98f
5139fc8
39b3c66
d47fa37
a50d0e8
6f595a9
ff9cfcb
1e4b899
fa1b1c3
f481c08
4a43464
6c1c40e
12d51d7
8fcefcf
791ab4a
b65f61a
8cf976e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,11 @@ import React, { | |
| import classNames from 'classnames'; | ||
|
|
||
| import { CommonProps } from '../../common'; | ||
| import { keys, DEFAULT_VISUALIZATION_COLOR } from '../../../services'; | ||
| import { | ||
| keys, | ||
| DEFAULT_VISUALIZATION_COLOR, | ||
| getSteppedGradient, | ||
| } from '../../../services'; | ||
| import { EuiColorStopThumb, ColorStop } from './color_stop_thumb'; | ||
| import { | ||
| addStop, | ||
|
|
@@ -45,6 +49,8 @@ import { EuiScreenReaderOnly } from '../../accessibility'; | |
| import { EuiRangeHighlight } from '../../form/range/range_highlight'; | ||
| import { EuiRangeTrack } from '../../form/range/range_track'; | ||
| import { EuiRangeWrapper } from '../../form/range/range_wrapper'; | ||
| // import { colorPalette } from '../../../services/color/color_palette'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused imports, please remove |
||
| // import { EuiColorPaletteDisplayFixed } from '../color_palette_display/color_palette_display_fixed'; | ||
|
|
||
| export interface EuiColorStopsProps extends CommonProps { | ||
| addColor?: ColorStop['color']; | ||
|
|
@@ -62,7 +68,8 @@ export interface EuiColorStopsProps extends CommonProps { | |
| max?: number; | ||
| min?: number; | ||
| label: string; | ||
| stopType?: 'fixed' | 'gradient'; | ||
| stopType?: 'fixed' | 'gradient' | 'stepped'; | ||
| stepNumber?: number; | ||
| mode?: EuiColorPickerProps['mode']; | ||
| swatches?: EuiColorPickerProps['swatches']; | ||
| showAlpha?: EuiColorPickerProps['showAlpha']; | ||
|
|
@@ -139,6 +146,7 @@ export const EuiColorStops: FunctionComponent<EuiColorStopsProps> = ({ | |
| className, | ||
| label, | ||
| stopType = 'gradient', | ||
| stepNumber = 10, | ||
| swatches, | ||
| showAlpha = false, | ||
| }) => { | ||
|
|
@@ -291,7 +299,6 @@ export const EuiColorStops: FunctionComponent<EuiColorStopsProps> = ({ | |
| if (isNotInteractive || isTargetAThumb(e.target) || !wrapperRef) return; | ||
| const newStop = getStopFromMouseLocationFn({ x: e.pageX, y: e.pageY }); | ||
| const newColorStops = addDefinedStop(colorStops, newStop, addColor); | ||
|
|
||
| setFocusStopOnUpdate(newStop); | ||
| handleOnChange(newColorStops); | ||
| }; | ||
|
|
@@ -440,10 +447,30 @@ export const EuiColorStops: FunctionComponent<EuiColorStopsProps> = ({ | |
| )}`; | ||
| } | ||
| }; | ||
| const linearGradient = sortedStops.map( | ||
| stopType === 'gradient' ? gradientStop : fixedStop | ||
| ); | ||
| const background = `linear-gradient(to right,${linearGradient})`; | ||
|
|
||
| let gradient: string = ''; | ||
|
|
||
| if (stopType === 'stepped') { | ||
| const trailingPercentage = colorStops[0].stop; | ||
| const steppedColors = getSteppedGradient(colorStops, stepNumber); | ||
| let steppedGradient = ''; | ||
| const percentage = (100 - trailingPercentage) / steppedColors.length; | ||
|
anuragxxd marked this conversation as resolved.
Outdated
|
||
| let percentageSteps = | ||
| (100 - trailingPercentage) / steppedColors.length + trailingPercentage; | ||
| steppedColors.forEach((color) => { | ||
| steppedGradient = steppedGradient.concat( | ||
| `${color} ${percentageSteps - percentage}% ${percentageSteps}%, ` | ||
| ); | ||
| percentageSteps = percentageSteps + percentage; | ||
| }); | ||
| steppedGradient = steppedGradient.substring(0, steppedGradient.length - 2); | ||
| gradient = `linear-gradient(to right, currentColor ${trailingPercentage}%, ${steppedGradient})`; | ||
| } else { | ||
| const linearGradient = sortedStops.map( | ||
| stopType === 'gradient' ? gradientStop : fixedStop | ||
| ); | ||
| gradient = `linear-gradient(to right,${linearGradient})`; | ||
| } | ||
|
|
||
| return ( | ||
| <EuiRangeWrapper | ||
|
|
@@ -483,7 +510,7 @@ export const EuiColorStops: FunctionComponent<EuiColorStopsProps> = ({ | |
| max={max || rangeMax} | ||
| lowerValue={min || rangeMin} | ||
| upperValue={max || rangeMax} | ||
| background={background} | ||
| background={gradient} | ||
| compressed={compressed} | ||
| /> | ||
| <div | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. 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 chroma from 'chroma-js'; | ||
| import { ColorStop } from '../../components/color_picker/color_stops'; | ||
|
|
||
| export const getSteppedGradient = function ( | ||
| colors: ColorStop[], | ||
| steps: number | ||
| ) { | ||
| const finalStops = [0, ...colors.map((item) => item.stop / 100), 1]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @git-anurag-hub This logic always assumes a The color segments should be done between the first and last stop |
||
| const color = [ | ||
| colors[0].color, | ||
| ...colors.map((item) => item.color), | ||
| colors[colors.length - 1].color, | ||
| ]; | ||
| return chroma.scale(color).domain(finalStops).colors(steps); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.