---
title: "replaceCurrentColor"
description: "Fetch an SVG and replace its currentColor references with a given color, returning the recolored markup as a string."
canonical_url: "https://core.mhaibaraai.cn/en/docs/helpers/file/replace-current-color"
---
# replaceCurrentColor

> Fetch an SVG and replace its currentColor references with a given color, returning the recolored markup as a string.

## Usage

`replaceCurrentColor` fetches an SVG file and replaces all `currentColor` references with a specified color.

> [!WARNING]
> 
> Note
> 
> - Requires a browser environment (`DOMParser` and `XMLSerializer`)
> - Uses `fetch` to retrieve the SVG file

```ts
import { replaceCurrentColor } from '@movk/core'

// Fetch and replace currentColor in an SVG
try {
  const svgContent = await replaceCurrentColor('/icons/star.svg', '#ff0000')
  // Create a Blob and display
  const blob = new Blob([svgContent], { type: 'image/svg+xml' })
  const url = URL.createObjectURL(blob)

  const img = document.createElement('img')
  img.src = url
  document.body.appendChild(img)
}
catch (error) {
  console.error('SVG processing failed:', error)
}

// Fetch SVG content without replacing colors
const originalSvg = await replaceCurrentColor('/icons/star.svg')
```

## API

### `replaceCurrentColor(path, color?)`

Replace `currentColor` references in an SVG file.

### Parameters

**path** (`string`) *required*: The path to the SVG file.

**color** (`string`): The replacement color value. If omitted, the original SVG is returned unchanged.

### Returns

**returns** (`Promise<string>`): The processed SVG markup as a string.

## Changelog

See commit history for [src/helpers/file/replaceCurrentColor.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/helpers/file/replaceCurrentColor.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/helpers/file/replaceCurrentColor.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
