# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import annotations

import tensorflow as tf

from tensorflow_similarity.augmenters.augmentation_utils.random_apply import (
    random_apply,
)
from tensorflow_similarity.types import Tensor


def random_solarize(
    image: Tensor,
    p: float = 0.2,
    pixel_min: int = 0,
    pixel_max: int = 255,
    thresh: int = 10,
) -> Tensor:
    def _transform(image: Tensor) -> Tensor:
        return tf.where(image < thresh, image, pixel_max - image)

    return random_apply(_transform, p=p, x=image)
