def _transform(self, x: "tf.Tensor", y: "tf.Tensor" | None, **kwargs) -> tuple["tf.Tensor", "tf.Tensor" | None]:
        """
        Transformation of an image with randomly sampled contrast.

        :param x: Input samples.
        :param y: Label of the samples `x`.
        :return: Transformed samples and labels.
        """
        import tensorflow as tf

        contrast_factor_i = np.random.uniform(low=self.contrast_factor_range[0], high=self.contrast_factor_range[1])
        if x.shape[3] == 3:
            red, green, blue = x[:, :, :, 0], x[:, :, :, 1], x[:, :, :, 2]
            x_gray = 0.2989 * red + 0.587 * green + 0.114 * blue
        elif x.shape[3] == 1:
            x_gray = x[:, :, :, 0]
        else:  # pragma: no cover
            raise ValueError("Number of color channels is not 1 or 3 in input `x` of format HWC.")
        mean = tf.math.reduce_mean(x_gray, axis=None)

        return (
            tf.clip_by_value(
                contrast_factor_i * x + (1.0 - contrast_factor_i) * mean,
                clip_value_min=self.clip_values[0],
                clip_value_max=self.clip_values[1],
            ),
            y,
        )

        Transformation of an image with randomly sampled contrast.

        :param x: Input samples.
        :param y: Label of the samples `x`.
        :return: Transformed samples and labels.
        
art/preprocessing/expectation_over_transformation/natural_corruptions/contrast/tensorflow.py