-
Notifications
You must be signed in to change notification settings - Fork 0
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
Support gradients for slide background #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
*/ | ||
|
||
import { EMU, REGEX_HEX_COLOR, DEF_FONT_COLOR, ONEPT, SchemeColor, SCHEME_COLORS } from './core-enums' | ||
import { IChartOpts, PresLayout, TextGlowProps, PresSlide, ShapeFillProps, Color, ShapeLineProps, Coord } from './core-interfaces' | ||
import { IChartOpts, PresLayout, TextGlowProps, PresSlide, ShapeFillProps, Color, ShapeLineProps, Coord, Gradient, GradientStops } from './core-interfaces' | ||
|
||
/** | ||
* Translates any type of `x`/`y`/`w`/`h` prop to EMU | ||
|
@@ -198,6 +198,66 @@ export function createGlowElement(options: TextGlowProps, defaults: TextGlowProp | |
return strXml | ||
} | ||
|
||
/** | ||
* Create gradient elements i.e. `a:gsLst` and `a:lin` | ||
* @param {Gradient} gradient | ||
* @param {string} internalElements | ||
* @see http://officeopenxml.com/drwSp-GradFill.php | ||
* @returns XML string | ||
*/ | ||
function createGradientElements(gradient: Gradient, internalElements: string): string { | ||
let strXml = '', | ||
stops = gradient.stops | ||
|
||
if (!isGradient(gradient)) { | ||
console.warn(`This is not a valid gradient:`, gradient) | ||
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. This is handled similar to colors. When no valid color is found, we log a warning in the console, and use a default color. See |
||
console.warn(`Fallback to default gradient with "${SchemeColor.background1}" and "${SchemeColor.background2}".`) | ||
console.warn(`Please provide a valid gradient e.g. { angle:0, stops: { 0:"#FFFFFF", 100:"#000000" } }.`) | ||
stops = { 0: SchemeColor.background1, 100: SchemeColor.background2 } | ||
} | ||
|
||
strXml += createGradientList(stops, internalElements) | ||
strXml += createLinearGradient(gradient.angle) | ||
|
||
return strXml | ||
} | ||
|
||
/** | ||
* Create gradient list element i.e. `a:gsLst` | ||
* @param {Gradient} gradient | ||
* @param {string} internalElements | ||
* @returns XML string | ||
*/ | ||
function createGradientList(stops: GradientStops, internalElements: string): string { | ||
const multiplier = 1000 | ||
const res = Object.keys(stops).map(pos => `<a:gs pos="${Number(pos) * multiplier}">${createColorElement(stops[pos], internalElements)}</a:gs>`) | ||
return `<a:gsLst>${res.join('')}</a:gsLst>` | ||
} | ||
|
||
/** | ||
* Create linear gradient element i.e. `a:lin` | ||
* @param {number} angle | ||
* @returns XML string | ||
*/ | ||
function createLinearGradient(angle = 0): string { | ||
const multiplier = 60000 | ||
return `<a:lin ang="${angle * multiplier}"/>` | ||
} | ||
|
||
/** | ||
* Validate gradient | ||
* @param {Gradient} gradient | ||
* @returns boolean | ||
*/ | ||
function isGradient(gradient?: Gradient): boolean { | ||
return Boolean( | ||
gradient?.stops && | ||
typeof gradient.stops === 'object' && | ||
Object.keys(gradient.stops).length > 0 && | ||
Object.keys(gradient.stops).every(stop => Number.isFinite(Number(stop))) | ||
) | ||
} | ||
|
||
/** | ||
* Create color selection | ||
* @param {Color | ShapeFillProps | ShapeLineProps} props fill props | ||
|
@@ -213,6 +273,7 @@ export function genXmlColorSelection(props: Color | ShapeFillProps | ShapeLinePr | |
if (typeof props === 'string') colorVal = props | ||
else { | ||
if (props.type) fillType = props.type | ||
if (props.gradient) fillType = 'gradient' | ||
if (props.color) colorVal = props.color | ||
if (props.alpha) internalElements += `<a:alpha val="${Math.round((100 - props.alpha) * 1000)}"/>` // DEPRECATED: @deprecated v3.3.0 | ||
if (props.transparency) internalElements += `<a:alpha val="${Math.round((100 - props.transparency) * 1000)}"/>` | ||
|
@@ -222,6 +283,9 @@ export function genXmlColorSelection(props: Color | ShapeFillProps | ShapeLinePr | |
case 'solid': | ||
outText += `<a:solidFill>${createColorElement(colorVal, internalElements)}</a:solidFill>` | ||
break | ||
case 'gradient': | ||
outText += `<a:gradFill>${createGradientElements((props as ShapeFillProps).gradient, internalElements)}</a:gradFill>` | ||
break | ||
default: // @note need a statement as having only "break" is removed by rollup, then tiggers "no-default" js-linter | ||
outText += '' | ||
break | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very imperative code here, but consistent with the style used in the project, see
createGlowElement
above