> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Establecimiento de colores en un LampArray

> Establecimiento de colores en un LampArray

Todas las funciones de actualización de color de ILampArray usan la estructura [LampArrayColor](/reference/lighting/lamparray/structs/lamparraycolor) para representar un valor de color RGBA.

El valor alfa (LampArrayColor::a) representa la transparencia relativa de un color, donde cero es totalmente transparente y 0xFF es totalmente opaco. Si se usa un valor alfa distinto de 0xFF, el color se someterá a un paso adicional de mezcla con el negro. Para evitar este paso de mezcla, no pase un valor alfa distinto de 0xFF al establecer los colores.

Las API SetColor de ILampArray están diseñadas para llamarse desde un único subproceso para obtener el mejor rendimiento y coherencia del estado de las lámparas.

## Actualización de todas las lámparas

El método [ILampArray::SetColor](/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolor) cambia todas las lámparas del dispositivo para que coincidan con el color deseado.

El ejemplo siguiente registra una devolución de llamada de LampArray y establece el color de cada dispositivo conectado.

```cpp theme={null}
const LampArrayColor greenColor = { 0x0 /* r */, 0xFF /* g */, 0x0 /* b */, 0xFF /* a */};
std::vector<Microsoft::WRL::ComPtr<ILampArray>> lampArrays;

void MyLampArrayStatusCallback(
    _In_opt_ void* context,
    LampArrayStatus currentStatus,
    LampArrayStatus previousStatus,
    _In_ ILampArray* lampArray)
{
    const bool wasAttached = (previousStatus & LampArrayStatus::Connected) == LampArrayStatus::Connected;
    const bool isAttached = (currentStatus & LampArrayStatus::Connected) == LampArrayStatus::Connected;
    if (wasAttached != isAttached)
    {
        if (isAttached)
        {
            lampArray->SetColor(greenColor);
            lampArrays.push_back(lampArray);
        }
        else
        {
            for (auto iter = lampArrays.begin(); iter != lampArrays.end(); )
            {
                if (iter->Get() == lampArray)
                {
                    lampArrays.erase(iter);
                }
                else
                {
                    iter++;
                }
            }
        }
    }
}

void MainLoop(
    _In_ volatile bool & cancelMonitoring) noexcept
{
    LampArrayCallbackToken token = LAMPARRAY_INVALID_CALLBACK_TOKEN_VALUE;
    if (SUCCEEDED(RegisterLampArrayStatusCallback(
        MyLampArrayStatusCallback,
        LampArrayEnumerationKind::Async,
        nullptr /* context */,
        &token)))
    {
        while (!cancelMonitoring)
        {
            Sleep(100);
        }

        UnregisterLampArrayCallback(token, 5000);
    }
}
```

## Selección de lámparas individuales o grupos de lámparas

[ILampArray::SetColorsForIndices](/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolorsforindices) se usa para actualizar una o más lámparas de un LampArray. Los índices de lámpara pasados a esta API pueden estar en cualquier orden.

El ejemplo siguiente ilustra cómo establecer colores alternos en todas las lámparas de un LampArray.

```cpp theme={null}
void SetAlternatingColors(ILampArray* lampArray)
{
    const LampArrayColor blueColor = { 0x0 /* r */, 0x0 /* g */, 0xFF /* b */, 0xFF /* a */};
    const LampArrayColor redColor = { 0xFF /* r */, 0x0 /* g */, 0x0 /* b */, 0xFF /* a */};

    const uint32_t lampCount = lampArray->GetLampCount();

    // Set up our index and color buffers
    std::vector<uint32_t> indicesBuffer(lampCount);
    std::vector<LampArrayColor> colorsBuffer(lampCount);

    // Populate the buffers
    for (uint32_t i = 0; i < lampCount; i++)
    {
        // We will use all the Lamps for this update.
        indicesBuffer[i] = i;

        // Odd numbered indices will be red, even numbered indices will be blue
        if (i % 2 != 0)
        {
            colorsBuffer[i] = redColor;
        }
        else
        {
            colorsBuffer[i] = blueColor;
        }
    }

    // Apply the colors to the LampArray
    lampArray->SetColorsForIndices(lampCount, indicesBuffer.data(), colorsBuffer.data());
}
```

El ejemplo siguiente muestra cómo actualizar los colores de lámparas individuales o de grupos de lámparas en un LampArray.

```cpp theme={null}
void MyCustomColorUpdate(ILampArray* lampArray)
{
    const LampArrayColor greenColor = { 0x0 /* r */, 0xFF /* g */, 0x0 /* b */, 0xFF /* a */};
    const LampArrayColor yellowColor = { 0xFF /* r */, 0xFF /* g */, 0x0 /* b */, 0xFF /* a */};
    const LampArrayColor whiteColor = { 0xFF /* r */, 0xFF /* g */, 0xFF /* b */, 0xFF /* a */};

    const uint32_t lampCount = lampArray->GetLampCount();

    // Set custom colors for a single lamp at index 4
    const uint32_t index = 4;
    lampArray->SetColorsForIndices(1, &index, &greenColor);

    // Simultaneously make Lamp 1 yellow and Lamp 3 white
    std::vector<uint32_t> indicesBuffer = { 1, 3 };
    std::vector<LampArrayColor> colorsBuffer = { yellowColor, whiteColor };

    // Apply the colors to the LampArray
    lampArray->SetColorsForIndices(static_cast<uint32_t>(indicesBuffer.size()), indicesBuffer.data(), colorsBuffer.data());
}
```

## Selección de lámparas mediante códigos de digitalización del teclado

[ILampArray::SetColorsForScanCodes](/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolorsforscancodes) proporciona una manera sencilla de establecer los colores de las lámparas de teclas específicas en un teclado LampArray. Esto puede ser útil para ayudar a enseñar a los usuarios qué teclas presionar en un tutorial, por ejemplo, o para mostrar información relacionada con el estado del juego en el teclado.

El ejemplo siguiente ilustra cómo cambiar el color de la lámpara de las teclas WASD en un teclado LampArray:

```cpp theme={null}
#define SC_W    0x11
#define SC_A    0x1E
#define SC_S    0x1F
#define SC_D    0x20

void UpdateWASDKeys(ILampArray* lampArray)
{
    const LampArrayColor blueColor = { 0x0 /* r */, 0x0 /* g */, 0xFF /* b */, 0xFF /* a */};
    const LampArrayColor yellowColor = { 0xFF /* r */, 0xFF /* g */, 0x0 /* b */, 0xFF /* a */};

    // Set the color for all lamps. We will override the WASD keys below.
    lampArray->SetColor(blueColor);

    // Set up the buffer of scan codes for the Lamps we want to target
    std::vector<uint32_t> scanCodesBuffer = { SC_W, SC_A, SC_S, SC_D };

    // Create a matching buffer of LampArrayColors. In this example, we will set all of the WASD keys to yellow.
    std::vector<LampArrayColor> colorsBuffer;
    for (size_t i = 0; i < scanCodesBuffer.size(); i++)
    {
        colorsBuffer.push_back(yellowColor);
    }

    // Set the color for the WASD keys
    lampArray->SetColorsForScanCodes(static_cast<uint32_t>(scanCodesBuffer.size()), scanCodesBuffer.data(), colorsBuffer.data());
}
```

## Consulte también

[Información general de la API de iluminación](/build/core-features/common/lighting/gc-lighting-toc)
[Referencia de ILampArray](/reference/lighting/lamparray/interfaces/ilamparray/ilamparray)
[LampArrayColor](/reference/lighting/lamparray/structs/lamparraycolor)
[ILampArray::SetColor](/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolor)
[ILampArray::SetColorsForIndices](/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolorsforindices)
[ILampArray::SetColorsForScanCodes](/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolorsforscancodes)


## Related topics

- [Referencia de la estructura LampArrayColor](/es/reference/lighting/lamparray/structs/lamparraycolor.md)
- [API de iluminación y LampArray en XBOX y PC](/es/build/core-features/common/lighting/index.md)
- [Método ILampArray::SetColor](/es/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolor.md)
- [Método ILampArray::SetColorsForIndices](/es/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolorsforindices.md)
- [Método ILampArray::SetColorsForScanCodes](/es/reference/lighting/lamparray/interfaces/ilamparray/methods/ilamparray_setcolorsforscancodes.md)
