Urysohn's function on a metric space

Consider $f: X \to [0,1]$ given by

$$f(x) = \frac {d(x,A)} {d(x,A) + d(x,B^c)} \ .$$

$f$ is well-defined because the denominator is $0$ if and only if $x \in A \cap B^c = \emptyset$.

We have $f(x) = 0 \iff x \in A$ and $f(x) = 1 \iff x \in B^c$.


EDIT 2: As pointed out by Theo, my original solution will work only if $d(A,B^c)>0$. (E.g. if $A$ or $B^c$ is compact.) However, the solution taken from Amman's and Escher's book (see EDIT 1 below) works for arbitrary $A$ and $B$.

Let us denote $\delta=d(A,B^c)$.

Let us define $f(x)=\frac1\delta d(x,A)$. This functions is continuous, $f(a)=0$ for $a\in A$ and $f(b)\ge 1$ for $b\in B^c$.

Similarly define $g(x)=\frac1\delta d(x,B^c)$.

Now define $f'(x)=\min\{1,f(x)\}$ and $g'(x)=\min\{1,g(x)\}$. These functions have similar properties, and additionally they have values in $[0,1]$.

There are several choices for the Urysohn function for $A$ and $B^c$:

$\frac{f'(x)+(1-g'(x))}2$, $\max\{f'(x),1-g'(x)\}$, $\min\{f'(x),1-g'(x)\}$.

I hope I have not overseen some mistake in the above reasoning.

EDIT 1: According to Proposition 4.13 in Amman, Escher: Analysis III, another such function is $\frac{d(x,A)}{d(x,A)+d(x,B^c)}$. I have to admit that this is much more elegant construction than mine. (In my notation it is $\frac{f(x)}{f(x)+g(x)}$.)