<commit_before>import Html exposing (..)
import Html.Attributes exposing (type_, value)
import Html.Events exposing (onClick)
import Canvas exposing (Canvas, Position, Image, Error, Size)
import Color
import Array exposing (Array)
import Task


main = 
  Html.program
  { init  = (initModel, initCmd) 
  , view   = view 
  , update = update
  , subscriptions = always Sub.none
  }



-- TYPES



type Msg
  = ImageLoaded (Result Error Image)
  | Invert


initModel : Canvas
initModel =
  Size 770 770
  |>Canvas.initialize
  |>Canvas.fill Color.black


initCmd : Cmd Msg
initCmd =
  Task.attempt ImageLoaded (Canvas.loadImage "./agnes-martin-piece.png")




-- UPDATE



update : Msg -> Canvas -> (Canvas, Cmd Msg)
update message canvas =
  case message of 

    Invert ->
      (invertCanvas canvas, Cmd.none)

    ImageLoaded imageResult ->
      case Result.toMaybe imageResult of
        Just image ->
          let 

            newCanvas =
              Canvas.getImageSize image
              |>Canvas.initialize
              |>Canvas.drawImage image (Position 0 0)
          
          in
            (newCanvas, Cmd.none)

        Nothing ->
          (canvas, Cmd.none)


invertCanvas : Canvas -> Canvas
invertCanvas canvas = 
  let

    imageData =
      Canvas.getImageData canvas

    size = 
      Canvas.getCanvasSize canvas
  
  in
    Canvas.fromImageData size (invertColors imageData)


invertColors : Array Int -> Array Int
invertColors = 
  Array.indexedMap invertColor 


invertColor : Int -> Int -> Int
invertColor index colorValue =
  if index % 4 == 3 then
    colorValue
  else
    255 - colorValue



-- VIEW



view : Canvas -> Html Msg
view canvas =
  div []
  [ h1 [] [ text "Click on the canvas to invert its colors" ]
  , Canvas.toHtml [ onClick Invert ] canvas
  ]




<commit_msg>Changes how invert example works<commit_after>import Html exposing (..)
import Html.Attributes exposing (type_, value)
import Html.Events exposing (onClick)
import Canvas exposing (Canvas, Position, Image, Error, Size)
import Color
import Array exposing (Array)
import Task


main = 
  Html.program
  { init  = (Loading, loadImage) 
  , view   = view 
  , update = update
  , subscriptions = always Sub.none
  }



-- TYPES



type Msg
  = ImageLoaded (Result Error Image)
  | Invert


type Model
  = GotCanvas Canvas
  | Loading


loadImage : Cmd Msg
loadImage =
  Task.attempt ImageLoaded (Canvas.loadImage "./agnes-martin-piece.png")




-- UPDATE



update : Msg -> Model -> (Model, Cmd Msg)
update message model =
  case model of 

    Loading ->
      case message of 

        ImageLoaded result ->

          case Result.toMaybe result of
          
            Just image ->
              let 
                canvas =
                  Canvas.fromImage image
              in
                (GotCanvas canvas, Cmd.none)

            Nothing ->

              (Loading, loadImage)

        _ ->

          (model, Cmd.none)


    GotCanvas canvas ->
      case message of

        Invert ->

          (GotCanvas <| invertCanvas canvas, Cmd.none)


        _ ->

          (model, Cmd.none)


invertCanvas : Canvas -> Canvas
invertCanvas canvas = 
  let

    imageData =
      Canvas.getImageData canvas

    size = 
      Canvas.getCanvasSize canvas
  
  in
    Canvas.fromImageData size (invertColors imageData)


invertColors : Array Int -> Array Int
invertColors = 
  Array.indexedMap invertColor 


invertColor : Int -> Int -> Int
invertColor index colorValue =
  if index % 4 == 3 then
    colorValue
  else
    255 - colorValue



-- VIEW



view : Model -> Html Msg
view model =
  let

    body =
      case model of

        Loading ->
          p [] [ text "Loading image.."]

        GotCanvas canvas ->
          Canvas.toHtml
            [ onClick Invert ]
            canvas

  in
    div []
    [ h1 [] [ text "Click on the canvas to invert its colors" ]
    , body
    ]





{'fraction_non_alphanumeric': 0.08199233716475096, 'fraction_numerical': 0.005108556832694764, 'mean_word_length': 2.0053722179585574, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3723593', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1137, 'n_words': 361}