






'''simple docstring'''
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import CLIPImageProcessor


class    _snake_case   (				unittest.TestCase					):

					"""simple docstring"""





					def __init__(     self   , UpperCAmelCase__   , UpperCAmelCase__=7   , UpperCAmelCase__=3   , UpperCAmelCase__=18   , UpperCAmelCase__=30   , UpperCAmelCase__=400   , UpperCAmelCase__=True   , UpperCAmelCase__=None   , UpperCAmelCase__=True   , UpperCAmelCase__=None   , UpperCAmelCase__=True   , UpperCAmelCase__=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]   , UpperCAmelCase__=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]   , UpperCAmelCase__=True   , )      ->	List[str]:
										a_									=   size if size is not None else {'shortest_edge': 20}
										a_									=   crop_size if crop_size is not None else {'height': 18, 'width': 18}
										a_									=   parent
										a_									=   batch_size
										a_									=   num_channels
										a_									=   image_size
										a_									=   min_resolution
										a_									=   max_resolution
										a_									=   do_resize
										a_									=   size
										a_									=   do_center_crop
										a_									=   crop_size
										a_									=   do_normalize
										a_									=   image_mean
										a_									=   image_std
										a_									=   do_convert_rgb




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_center_crop": self.do_center_crop,
										    "crop_size": self.crop_size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_convert_rgb": self.do_convert_rgb,
										}




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__=False   , UpperCAmelCase__=False   , UpperCAmelCase__=False     )      ->	Tuple:
										assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

										if equal_resolution:
															a_									=   []
															for i in range(self.batch_size     ):
																				image_inputs.append(
																				    np.random.randint(
																				        255   , size=(self.num_channels, self.max_resolution, self.max_resolution)   , dtype=np.uinta     )     )
										else:
															a_									=   []
															for i in range(self.batch_size     ):
																				a_							,					a_									=   np.random.choice(np.arange(self.min_resolution   , self.max_resolution     )   , 2     )
																				image_inputs.append(np.random.randint(255   , size=(self.num_channels, width, height)   , dtype=np.uinta     )     )

										if not numpify and not torchify:
															# PIL expects the channel dimension as last dimension
															a_									=   [Image.fromarray(np.moveaxis(UpperCAmelCase__   , 0   , -1     )     ) for x in image_inputs]

										if torchify:
															a_									=   [torch.from_numpy(UpperCAmelCase__     ) for x in image_inputs]

										return image_inputs



@require_torch
@require_vision
class    _snake_case   (				snake_case     ,					unittest.TestCase					):

					"""simple docstring"""





					_UpperCamelCase		 =      CLIPImageProcessor if is_vision_available() else None




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_									=   CLIPImageProcessingTester(self     )




					@property
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										return self.image_processor_tester.prepare_image_processor_dict()




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[Any]:
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_resize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'size'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_center_crop'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'center_crop'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_normalize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_mean'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_std'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_convert_rgb'     )     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[str]:
										a_									=   self.image_processing_class.from_dict(self.image_processor_dict     )
										self.assertEqual(image_processor.size   , {'shortest_edge': 20}     )
										self.assertEqual(image_processor.crop_size   , {'height': 18, 'width': 18}     )

										a_									=   self.image_processing_class.from_dict(self.image_processor_dict   , size=42   , crop_size=84     )
										self.assertEqual(image_processor.size   , {'shortest_edge': 42}     )
										self.assertEqual(image_processor.crop_size   , {'height': 84, 'width': 84}     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										pass




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[str]:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PIL images
										a_									=   self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , Image.Image     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Union[str, Any]:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random numpy tensors
										a_									=   self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase__   , numpify=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , np.ndarray     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PyTorch tensors
										a_									=   self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase__   , torchify=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , torch.Tensor     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )



@require_torch
@require_vision
class    _snake_case   (				snake_case     ,					unittest.TestCase					):

					"""simple docstring"""





					_UpperCamelCase		 =      CLIPImageProcessor if is_vision_available() else None




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[Any]:
										a_									=   CLIPImageProcessingTester(self   , num_channels=4     )
										a_									=   3




					@property
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										return self.image_processor_tester.prepare_image_processor_dict()




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_resize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'size'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_center_crop'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'center_crop'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_normalize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_mean'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_std'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_convert_rgb'     )     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Union[str, Any]:
										pass




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[str]:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PIL images
										a_									=   self.image_processor_tester.prepare_inputs(equal_resolution=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , Image.Image     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.expected_encoded_image_num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.expected_encoded_image_num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    )   , )









'''simple docstring'''
def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[int]:






					"""simple docstring"""







					if hue < 0 or hue > 3_6_0:
										raise Exception('hue should be between 0 and 360' )

					if saturation < 0 or saturation > 1:
										raise Exception('saturation should be between 0 and 1' )

					if value < 0 or value > 1:
										raise Exception('value should be between 0 and 1' )

					a_									=   value * saturation
					a_									=   hue / 6_0
					a_									=   chroma * (1 - abs(hue_section % 2 - 1 ))
					a_									=   value - chroma

					if hue_section >= 0 and hue_section <= 1:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 1 and hue_section <= 2:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
					elif hue_section > 2 and hue_section <= 3:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
					elif hue_section > 3 and hue_section <= 4:
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					elif hue_section > 4 and hue_section <= 5:
										a_									=   round(2_5_5 * (second_largest_component + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (chroma + match_value) )
					else:
										a_									=   round(2_5_5 * (chroma + match_value) )
										a_									=   round(2_5_5 * (match_value) )
										a_									=   round(2_5_5 * (second_largest_component + match_value) )

					return [red, green, blue]

def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )				->				list[float]:






					"""simple docstring"""







					if red < 0 or red > 2_5_5:
										raise Exception('red should be between 0 and 255' )

					if green < 0 or green > 2_5_5:
										raise Exception('green should be between 0 and 255' )

					if blue < 0 or blue > 2_5_5:
										raise Exception('blue should be between 0 and 255' )

					a_									=   red / 2_5_5
					a_									=   green / 2_5_5
					a_									=   blue / 2_5_5
					a_									=   max(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   value - min(_UpperCAmelCase					,  _UpperCAmelCase					,  _UpperCAmelCase )
					a_									=   0 if value == 0 else chroma / value

					if chroma == 0:
										a_									=   0.0
					elif value == float_red:
										a_									=   6_0 * (0 + (float_green - float_blue) / chroma)
					elif value == float_green:
										a_									=   6_0 * (2 + (float_blue - float_red) / chroma)
					else:
										a_									=   6_0 * (4 + (float_red - float_green) / chroma)

					a_									=   (hue + 3_6_0) % 3_6_0

					return [hue, saturation, value]





def 							a	(   _UpperCAmelCase					,  _UpperCAmelCase )				->				bool:






					"""simple docstring"""







					a_									=   abs(hsv_a[0] - hsv_a[0] ) < 0.2
					a_									=   abs(hsv_a[1] - hsv_a[1] ) < 0.0_0_2
					a_									=   abs(hsv_a[2] - hsv_a[2] ) < 0.0_0_2

					return check_hue and check_saturation and check_value

