Skip to content

Commit a47b374

Browse files
committed
Remove color-interpolate and use AnimatedInterpolation instead
1 parent 767f7f2 commit a47b374

File tree

3 files changed

+21
-64
lines changed

3 files changed

+21
-64
lines changed

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
"react": "*",
2121
"react-native": "*"
2222
},
23-
"dependencies": {
24-
"color-interpolate": "^1.0.5"
25-
},
2623
"devDependencies": {
2724
"@types/react": "^16.9.19",
2825
"@types/react-native": "0.62.13",

src/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { Easing, EasingFunction } from 'react-native'
2-
import interpolate from 'color-interpolate'
3-
4-
const easeInOut = Easing.bezier(0.42, 0, 0.58, 1)
5-
const TOTAL_STOPS_PER_STEP = 16
1+
import { Easing, EasingFunction, Animated } from 'react-native'
2+
// @ts-expect-error
3+
import AnimatedInterpolation from 'react-native/Libraries/Animated/src/nodes/AnimatedInterpolation'
64

75
interface ColorStops {
86
[location: number]: {
@@ -16,6 +14,11 @@ interface GradientParams {
1614
easing?: EasingFunction
1715
}
1816

17+
type ColorInterpolateFunction = (input: number) => string
18+
19+
const easeInOut = Easing.bezier(0.42, 0, 0.58, 1)
20+
const TOTAL_STOPS_PER_STEP = 16
21+
1922
function easeGradient({ colorStops, easing = easeInOut }: GradientParams) {
2023
const colors: string[] = []
2124
const locations: number[] = []
@@ -36,19 +39,28 @@ function easeGradient({ colorStops, easing = easeInOut }: GradientParams) {
3639

3740
const startColor = colorStops[startLocation].color
3841
const endColor = colorStops[endLocation].color
39-
const colorScale = interpolate([startColor, endColor])
42+
const currentEasing = colorStops[startLocation].easing ?? easing
43+
44+
const interpolationConfig: Animated.InterpolationConfigType = {
45+
inputRange: [0, 1],
46+
outputRange: [startColor, endColor],
47+
easing: currentEasing,
48+
}
49+
50+
const colorScale: ColorInterpolateFunction = AnimatedInterpolation.__createInterpolation(
51+
interpolationConfig
52+
)
4053

4154
const stepSize = endLocation - startLocation
4255
const frameSize = 1 / (TOTAL_STOPS_PER_STEP - 1)
43-
const currentEasing = colorStops[startLocation].easing ?? easing
4456

4557
for (
4658
let frameIndex = 0;
4759
frameIndex <= TOTAL_STOPS_PER_STEP - 1;
4860
frameIndex++
4961
) {
5062
const progress = frameIndex * frameSize
51-
const color = colorScale(currentEasing(progress))
63+
const color = colorScale(progress)
5264
colors.push(color)
5365
locations.push(startLocation + stepSize * progress)
5466
}

yarn.lock

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,11 +1463,6 @@ ajv@^6.10.0, ajv@^6.12.4:
14631463
json-schema-traverse "^0.4.1"
14641464
uri-js "^4.2.2"
14651465

1466-
almost-equal@^1.1.0:
1467-
version "1.1.0"
1468-
resolved "https://registry.yarnpkg.com/almost-equal/-/almost-equal-1.1.0.tgz#f851c631138757994276aa2efbe8dfa3066cccdd"
1469-
integrity sha1-+FHGMROHV5lCdqou++jfowZszN0=
1470-
14711466
anser@^1.4.9:
14721467
version "1.4.10"
14731468
resolved "https://registry.yarnpkg.com/anser/-/anser-1.4.10.tgz#befa3eddf282684bd03b63dcda3927aef8c2e35b"
@@ -2023,11 +2018,6 @@ ci-info@^2.0.0:
20232018
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46"
20242019
integrity sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==
20252020

2026-
clamp@^1.0.1:
2027-
version "1.0.1"
2028-
resolved "https://registry.yarnpkg.com/clamp/-/clamp-1.0.1.tgz#66a0e64011816e37196828fdc8c8c147312c8634"
2029-
integrity sha1-ZqDmQBGBbjcZaCj9yMjBRzEshjQ=
2030-
20312021
class-utils@^0.3.5:
20322022
version "0.3.6"
20332023
resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"
@@ -2121,41 +2111,16 @@ color-convert@^2.0.1:
21212111
dependencies:
21222112
color-name "~1.1.4"
21232113

2124-
color-interpolate@^1.0.5:
2125-
version "1.0.5"
2126-
resolved "https://registry.yarnpkg.com/color-interpolate/-/color-interpolate-1.0.5.tgz#d5710ce4244bd8b9feeda003f409edd4073b6217"
2127-
integrity sha512-EcWwYtBJdbeHyYq/y5QwVWLBUm4s7+8K37ycgO9OdY6YuAEa0ywAY+ItlAxE1UO5bXW4ugxNhStTV3rsTZ35ZA==
2128-
dependencies:
2129-
clamp "^1.0.1"
2130-
color-parse "^1.2.0"
2131-
color-space "^1.14.3"
2132-
lerp "^1.0.3"
2133-
21342114
color-name@1.1.3:
21352115
version "1.1.3"
21362116
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
21372117
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=
21382118

2139-
color-name@^1.0.0, color-name@~1.1.4:
2119+
color-name@~1.1.4:
21402120
version "1.1.4"
21412121
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
21422122
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==
21432123

2144-
color-parse@^1.2.0:
2145-
version "1.4.2"
2146-
resolved "https://registry.yarnpkg.com/color-parse/-/color-parse-1.4.2.tgz#78651f5d34df1a57f997643d86f7f87268ad4eb5"
2147-
integrity sha512-RI7s49/8yqDj3fECFZjUI1Yi0z/Gq1py43oNJivAIIDSyJiOZLfYCRQEgn8HEVAj++PcRe8AnL2XF0fRJ3BTnA==
2148-
dependencies:
2149-
color-name "^1.0.0"
2150-
2151-
color-space@^1.14.3:
2152-
version "1.16.0"
2153-
resolved "https://registry.yarnpkg.com/color-space/-/color-space-1.16.0.tgz#611781bca41cd8582a1466fd9e28a7d3d89772a2"
2154-
integrity sha512-A6WMiFzunQ8KEPFmj02OnnoUnqhmSaHaZ/0LVFcPTdlvm8+3aMJ5x1HRHy3bDHPkovkf4sS0f4wsVvwk71fKkg==
2155-
dependencies:
2156-
hsluv "^0.0.3"
2157-
mumath "^3.3.4"
2158-
21592124
color-support@^1.1.3:
21602125
version "1.1.3"
21612126
resolved "https://registry.yarnpkg.com/color-support/-/color-support-1.1.3.tgz#93834379a1cc9a0c61f82f52f0d04322251bd5a2"
@@ -3439,11 +3404,6 @@ hosted-git-info@^3.0.6:
34393404
dependencies:
34403405
lru-cache "^6.0.0"
34413406

3442-
hsluv@^0.0.3:
3443-
version "0.0.3"
3444-
resolved "https://registry.yarnpkg.com/hsluv/-/hsluv-0.0.3.tgz#829107dafb4a9f8b52a1809ed02e091eade6754c"
3445-
integrity sha1-gpEH2vtKn4tSoYCe0C4JHq3mdUw=
3446-
34473407
http-cache-semantics@^4.0.0:
34483408
version "4.1.0"
34493409
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
@@ -4127,11 +4087,6 @@ language-tags@^1.0.5:
41274087
dependencies:
41284088
language-subtag-registry "~0.3.2"
41294089

4130-
lerp@^1.0.3:
4131-
version "1.0.3"
4132-
resolved "https://registry.yarnpkg.com/lerp/-/lerp-1.0.3.tgz#a18c8968f917896de15ccfcc28d55a6b731e776e"
4133-
integrity sha1-oYyJaPkXiW3hXM/MKNVaa3Med24=
4134-
41354090
leven@^3.1.0:
41364091
version "3.1.0"
41374092
resolved "https://registry.yarnpkg.com/leven/-/leven-3.1.0.tgz#77891de834064cccba82ae7842bb6b14a13ed7f2"
@@ -4793,13 +4748,6 @@ ms@2.1.2:
47934748
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
47944749
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
47954750

4796-
mumath@^3.3.4:
4797-
version "3.3.4"
4798-
resolved "https://registry.yarnpkg.com/mumath/-/mumath-3.3.4.tgz#48d4a0f0fd8cad4e7b32096ee89b161a63d30bbf"
4799-
integrity sha1-SNSg8P2MrU57Mglu6JsWGmPTC78=
4800-
dependencies:
4801-
almost-equal "^1.1.0"
4802-
48034751
mute-stream@0.0.7:
48044752
version "0.0.7"
48054753
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"

0 commit comments

Comments
 (0)